Alvaro Fuentes Zurita
Alvaro Fuentes Zurita

Reputation: 434

Nginx locations and nested Slim indexes

Using php Slim framework and having the following structure:

/root
    /dir_parent_1/
        /dir_child_1
            /index.php
    /dir_parent_2
        /index.php

/root/dir_parent_1/dir_child_1/index.php have the users route, and /root/dir_parent_2/index.php have the clients route.

How i write the Nginx location to match the two possible combinations?

I try:

location ~ ^/root/(\w+)
{
    try_files $uri /gestionhospitales/api/$1/index.php?$query_string;
}

location ~ ^/root/(\w+)/(\w+)
{
    try_files $uri /gestionhospitales/api/$1/$2/index.php?$query_string;
}

But they are mutually excluding. Going to "/root/dir_parent_1/dir_child_1/users" match the first rule (because it is first. If i invert the order, the second is matched),

Also i started to write a rule for both cases, but i don't know how to finish it:

location ~ ^/root/(\w+)?/(\w*)
{
    try_files $uri /gestionhospitales/api/$1/$2/index.php?$query_string;
#  tried with if, rewrite, etc. But i'm not nginx expert.
}

Thanks in advance.

Upvotes: 1

Views: 444

Answers (1)

robinef
robinef

Reputation: 312

The only route you have to setup is to forward everything to index.php file in your public directory.

root         /path/www.mysite.com/public_html;

try_files $uri /index.php;

This file should contains the php code to boostrap Slim Framework. Then all "routes" will be handled by PHP and Slim according to your code in index.php and not by nginx.

$app->get('/users', function () {
    echo "This is the user route!";
});

$app->get('/clients', function () {
    echo "This is the client route!";
});

You can see a complete example here in the official documentation.

Upvotes: 1

Related Questions