We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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/some_url_that_does_not_exist' it still shows the app/views/index/index.phtml.

Dir tree:


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

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


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

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.



3.0k

I added router to the DI


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

and a route file app/routes/routes.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['REQUEST_URI']);

/*
echo 'Testing ', $_SERVER['REQUEST_URI'], '<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


$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.



98.9k

$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



3.0k

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

Now my routes.php looks like this


<?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?



8.1k

Yes, may be. Your nginx config does not create spesial variable url. Phalcon read parameters, including routes, from this variable. Directive

$router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);

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



3.0k

Thank you, Oleg!

That solved the problem.