Routing in yii2

Hi guys,

following routing-rules work fine under localhost/XAMPP(Apache), but it fails under LINUX(nginx) by throwing out 404-Error.What are differences in defining rules in yii2 as regards these two Operation Systems?


...

'urlManager' => [

            'class' => 'yii\web\UrlManager',

            'enablePrettyUrl' => true,

            'showScriptName' => true,

            'enableStrictParsing' => true,

            'rules' => [

                '/' => 'site/login',

                'reset'=>'site/request-password-reset',

                'about' => 'site/index',

                'contact' => 'site/contact',

                'logout' => 'site/logout',

                'signup' => 'site/signup',

                'formular' => 'site/script',

                'praktikum' => 'bewerbungen/index',

                '<controller:\w+>/<id:\d+>' => '<controller>/view',

                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

                'country' => 'country/index'

            ],

...



If necessary,here are the config files of nginx




root@WSL-Server:/etc/nginx/sites-available#cat Default


  #}

}





root@WSL-Server:/etc/nginx/sites-available# clear

root@WSL-Server:/etc/nginx/sites-available# cat default


server {

        listen 80 default_server;

        listen [::]:80 default_server;


        root /var/www/html;


        index index.html index.htm index.nginx-debian.html;


        server_name _;


        location / {

             

                try_files $uri $uri/ =404;

        }




}



and another one under OpenMediaVault:




rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;

try_files $uri $uri/ index.php;

}

location ~ ^(.+?\.php)(/.*)?$ {

try_files $1 = 404;

include fastcgi_params;

fastcgi_param SCRIPT_FILENAME $document_root$1;

fastcgi_param PATH_INFO $2;

fastcgi_param HTTPS on;

fastcgi_pass $socket;

}

# Optional: set long EXPIRES header on static assets

location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {

expires 30d;

# Optional: Don't log access to assets

access_log off;

}

}


server {

    listen 1027;

    listen [::]:1027;

    set $root_path "/srv/dev-disk-by-label-appServer/Yii2_Mail/Yii2_Mail/frontend/web";

    root $root_path;

    index yiic.php;

    set $socket "unix:/var/run/fpm-36a313b9-da05-4c39-b71e-ade398730c14.sock";

    location ~ \.php$ {

        include snippets/fastcgi-php.conf;

        fastcgi_pass $socket;

    }

    access_log /var/log/nginx/8bdd6dde-225b-4ba0-ab28-5806c9046194-access.log;

    error_log  /var/log/nginx/8bdd6dde-225b-4ba0-ab28-5806c9046194-error.log;

    large_client_header_buffers 4 8k;

}



Linux is usually more case sensitive so check your controller file names.

Which controller file names? - There is no Controller involved in, just main-local.php in Folder common/config!

It’s not a problem with the url manager’s rule.

For example, a route “abcd/index” will be resolved to “abcd” controller’s “index” action, and “abcd” controller should have the source file named “AbcdController.php” by convention.

A Windows system will accept "abcdcontroller.php" as the source file, since it is case-insensitive. But a linux system will not. So, check your file names.

Si I did. Every files basically are declared lowercased in all my projects. Nevertheless, routing is failing under nginx, but not under apache…

This is your error. As @softark wrote filename are not all lovercase.

Yii resolve route to controller and action name.

Lets use a basic example

http://localhost/post/create

The route is [color="#0000FF"]post[/color]/[color="#FF8C00"]create[/color]

This url will point to the controller

[color="#0000FF"]PostController.php[/color]

Inside this file Yii will look for the action, a method of the class named as follow

function[b] [color="#FF8C00"]actionCreate[/color]/b {

}

A more complex example to better understand how controller/action name is matching a route

http://localhost/post-comment/add-like

the route is [color="#0000FF"]post-comment[/color]/[color="#FF8C00"]add-like[/color]

controller filename

[color="#0000FF"]PostCommentController.php[/color]

action name

[color="#FF8C00"]actionAddLike/color

Shortly filename are UpperCamelCase and action are lowerCamleCase.

In route the uppercase became lower case and an ‘-’ minus char is inserted to separate the words

To have yii working under linux or any other OS that has case sensitive file naming rename your file accordingly the above rules.

P.S.

do not forget to check models and view files as well.

P.P.S

if you are unsure about file naming, I suggest you to use gii to generate your code until you understand the rules.

Could just be that Apache/Xampp server has rewrite set on, and the nginx server doesn’t?

Gii won’t generate configfiles as described. How could I use gii to generate code in this case?

Gii does not generate config file, what do you mean exactly?

I think here is your problem, under linux (where ngingx is running) the file have wrong naming convention.

Also @jkofsky advice should be checked.

If the 404 not found is from nginx it is url rewrite is not configured proprerly

If the 404 not found is from YII probably is a filename problem

Yii 404 erorrs should be in site layout, nginx error are in very basic layout