All paths lead to index view

I decided to try something new and installed Phalcon on Nginx (haven't used this before) and started following the Tutorial 1: Let’s learn by example.

I have created the signup link to the index page. When I click that link I'm taken to page localhost/signup but the the index page is shown (I have created app/controllers/SignupController.php and app/views/signup/index.phtml).

No matter what I write to browser's address bar, it always leads to the same result. If I type in 'localhost/someurlthatdoesnot_exist' it still shows the app/views/index/index.phtml.

Dir tree: ```php

/var/www/phalcon/ app/ controllers/ models/ views/ public/ css/ img/ js/

Nginx conf (/etc/nginx/sites-available/default):

php

server {

listen   80;
server_name localhost;

index index.php index.html index.htm;

set $root_path '/var/www/phalcon/public';
root $root_path;

location / {
    try_files $uri $uri/ /index.php;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include fastcgi_params;
}

location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
    root $root_path;
}

location ~ /\.ht {
    deny all;
}

}

Bootstrap (/var/www/phalcon/public/index.php):

php

<?php

try {

//Register an autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
    '../app/controllers/',
    '../app/models/'
))->register();

//Create a DI
$di = new Phalcon\DI\FactoryDefault();

//Setting up the view component
$di->set('view', function(){
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir('../app/views/');
    return $view;
});

//Handle the request
$application = new \Phalcon\Mvc\Application($di);

echo $application->handle()->getContent();

} catch(\Phalcon\Exception $e) { echo "PhalconException: ", $e->getMessage(); }

```

There must be some small beginner's error somewhere.



1.9k

I added router to the DI ```php

$di->set('router', function(){ require DIR.'/../app/routes/routes.php'; return $router; });

and a route file app/routes/routes.php

php

<?php

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

$router->add("/", array(
'controller' => 'index', 'action' => 'index', ));

/$router->add("/:controller", array(
'controller' => '1', 'action' => 'index', ));
/

$router->add("/signup", array(
'controller' => 'signup', 'action' => 'index', ));

$router->handle($SERVER['REQUESTURI']);

/* echo 'Testing ', $SERVER['REQUESTURI'], '<br>';

//Check if some route was matched if ($router->wasMatched()) { echo 'Controller: ', $router->getControllerName(), '<br>'; echo 'Action: ', $router->getActionName(), '<br>'; } else { echo 'The route wasn\'t matched by any route<br>'; } echo '<br>'; */

but nothing changes. If I uncomment the testing part of the router file it claims to be using SignupController but it still shows the index page (even if the browser's address bar says we're on localhost/signup).

If I fix the router a bit

php

$router->add("/", array(
'controller' => 'signup', 'action' => 'index', ));

```

the signup page is shown. So at least the router file is read :)

But still the problem remains.



81.8k

$router->handle() must not be called manually, a proper routes.php looks like this: https://github.com/phalcon/forum/blob/master/app/config/routes.php



1.9k

Thank you for the proper routes.php but it didn't change anything.

Now my routes.php looks like this ```php

<?php

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

$router->add('/signup', array(
'controller' => 'signup', 'action' => 'index' ));

$router->add('/', array(
'controller' => 'index', 'action' => 'index' ));

return $router;

```

So for some reason the IndexController is read always, no matter what the url in browser's address bar says. Even if I try to go to a page that doesn't exist.

Would this be a problem in the Nginx config?



7.1k

Yes, may be. Your nginx config does not create spesial variable url. Phalcon read parameters, including routes, from this variable. Directive ```php $router->setUriSource(\Phalcon\Mvc\Router::URISOURCESERVERREQUESTURI); ``` solve this problem. Or change nginx config to create url parameter. This issue is covered in detail in the documentation. https://phalcon-php-framework-documentation.readthedocs.org/en/latest/reference/nginx.html?highlight=nginx#nginx-installation-notes



1.9k

Thank you, Oleg!

That solved the problem.