Can't call indexController's actions other

2019-05-01 20:04发布

问题:

I have a simple project in my xampp/htdocs directory called phalcon and I have apache configured to point to that folder so that I can go to phalcon/ in my browser.

The problem occurs when I try to open an index controller view other than index(default).
For instance I have someAction in Index controller and in views/index I have some.phtml.
If I go to phalcon/index/some I don't get the text from some.phtml outputed to the page.
It's probably because it thinks as if I wan't to open IndexController->indexAction and pass some as a parameter.

Would be grateful on any help provided to resolve this.
P.S. the project skeleton was copied from https://github.com/phalcon/skeleton-single.

Index controller:

<?php

class IndexController extends ControllerBase
{

  public function indexAction($action = null)
  {

  }
  public function someAction () {
      exit('test');
  }

}

views/index/index.phtml :

<?php echo $this->getContent(); ?>

views/index/some.phtml:

Some Action

views/index.phtml

<!DOCTYPE html>
<html>
<head>
  <title>Phalcon PHP Framework</title>
</head>
<body>
  <?php echo $this->getContent(); ?>
</body>
</html>

回答1:

I am not sure if I understand your question completely but I think your Action must match the folder/file in view with the same name for it to function.

Also, I see you have extends 'ControllerBase' there then do you have a file called 'ControllerBase' in the controllers file with:

class ControllerBase extends \Phalcon\Mvc\Controller 

Another thing you can check would be the index.php in your public folder. Most of the time these stuff happens because the DI is missing or with a wrong input.



回答2:

I think, that you should check your .htaccess file for following rule:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

This will enable all other controller actions, not only index. To check that the problem is in this rule you can by access your controller method via url /index.php?_url=/index/some - if it will be loaded, change your .htaccess like in example below



回答3:

Not sure if that's expected behaviour, but in some scenarios actions from IndexController should be requested in Capital case like Index/some/ when index/some/ would return not found response.



回答4:

Try using custom routing.

This can be done as follows if you are using the skeleton project but the concept is the same for any other application.

create the file app/config/routes.php

/**@var $router \Phalcon\Mvc\Router*/
$router = $di->get('router');

$router->add(
    "/index/some",
    [
       "controller" => "index",
       "action"     => "some",
    ]
 );

In the index.php after loading services.php

require __DIR__ . '/../app/config/routes.php';


回答5:

please change your .htaccess settings to following one

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule  ^$ public/    [L]
RewriteRule  (.*) public/$1 [L]
</IfModule>


回答6:

class SomeController...

is what you're looking for.