0

I've recently started to use Phalcon framework. It looks very good with one minor exception - I cannot make controllers work. This is what I have:

//IndexController.php

namespace MyNamespace\Controller;

use Phalcon\Mvc\Controller;

class IndexController extends Controller 
{
    //This is the only method that works
    public function indexAction()
    {
        echo "Hello from IndexController/indexAction";
    }

    //This does not work
    public function loginAction()
    {
        echo "Hello from IndexController/loginAction";
    }
}


//UserController.php
namespace MyNamespace\Controller;
...
//This does not work
public function loginAction()
{
    echo "Hello from UserController/loginAction";
}
...

So, when I go to localhost/ - everything is ok, I see this message - "Hello from IndexController/indexAction". However, when I go to localhost/login or localhost/user/login I see the very same error message, that the requested url was not found. I'm not sure what is wrong with that and how can I fix it.

EDIT

I've just created routes.php file and use it inside my bootstrap file. However, I see that for some reason all routes except / are ignored. So, this is how it looks like:

...
$router = new Phalcon\Mvc\Router(false);

$router->add(
    '/login',
    [
        'namespace' => 'MyNamespace\Controller',
        'controller' => 'user',
        'action' => 'login'
    ]
);

$router->add(
    '/',
    [
        'namespace' => 'MyNamespace\Controller',
        'controller' => 'user',
        'action' => 'login'
    ]
);

So, I see that this routes.php is indeed used, because when I go to localhost I now see a message from UserController loginAction. However, when I go to localhost/login I still see that notorious "not found" error message. I really wonder, why Phalcon ignores all routes except /.

EDIT

So, I echoed phpinfo in my indexAction and see this under Configuration title:

Loaded Modules: core mod_win32 mpm_winnt http_core mod_so mod_php7 mod_access_compat mod_actions mod_alias mod_allowmethods mod_asis mod_auth_basic mod_authn_core mod_authn_file mod_authz_core mod_authz_groupfile mod_authz_host mod_authz_user mod_autoindex mod_cgi mod_dir mod_env mod_include mod_isapi mod_log_config mod_mime mod_negotiation mod_proxy mod_proxy_http mod_rewrite mod_setenvif

As you can see, mod_rewrite is enabled.

EDIT

And this is how my apache configuration files look like:

#httpd.conf

<IfModule mod_rewrite>

    <Directory c:/Apache242/htdocs/MyApp/Framework>
      RewriteEngine on
      RewriteRule  ^$ public/    [L]
      RewriteRule  ((?s).*) public/$1 [L]
    </Directory>

    <Directory c:/Apache242/htdocs/MyApp/Framework/public>
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
    </Directory>

</IfModule>

#httpd-vhosts.conf

<VirtualHost *:8080>
  DocumentRoot "c:/Apache242/htdocs/MyApp/Framework/public"
  ServerName ia.localhost
  <Directory c:/Apache242/htdocs/MyApp/Framework/public>
    Options All
    AllowOverride All
    Require all granted
  </Directory>
  DirectoryIndex
  <IfModule dir_module>
    DirectoryIndex index.php index.html index.htm
  </IfModule>
</VirtualHost>

It seems like I have all pieces of the puzzle, but it still does not work.

EDIT

The solution was to create htaccess file with such contents:

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

and put it just inside public folder. No other htaccess files and no other apache configuration is needed to make it work. Hope it will help someone.

Jacobian
  • 10,122
  • 29
  • 128
  • 221

0 Answers0