Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using different subdirectory #43

Open
matteovisotto opened this issue Mar 10, 2021 · 8 comments
Open

Using different subdirectory #43

matteovisotto opened this issue Mar 10, 2021 · 8 comments

Comments

@matteovisotto
Copy link

Hi, my project has the main folder with index.php file, then I would like to use the api subdirectory with an other index.php in order to manage the different routes with different files. I've tried to create the subdirectory, add an index, change the base path and write my routes. I've also add a location /app/api/ to my Nginx server configuration but it doesn't work, How can I do that? (the main routing system works perfectly)

@steampixel
Copy link
Owner

Hi @matteovisotto,
unfortunately I am not very familiar with Nginx server. So I don't know out of the box what the correct configuration is. Can you please share your configuration?

@matteovisotto
Copy link
Author

matteovisotto commented Mar 10, 2021

That's my configuration. At the moment is the default site and the routing system is under test path:


	#server configuration before this part (listen 80 and server name)

	# pass PHP scripts to FastCGI server
	#
	location ~ \.php$ {
		include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
		fastcgi_pass unix:/run/php/php7.4-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	}
	
	location /test/ {
  		if (!-e $request_filename){
    		rewrite ^(.*)$ /test/index.php;
 		 }
	}

location /test/api/{
  		if (!-e $request_filename){
    		rewrite ^(.*)$ /test/api/index.php;
 		 }
	}

	
	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /\.ht {
		deny all;
	}
}

I've noticed that adding location /test/api/ or deleting it the result is the same, a white page (not 404 from the main index) that could be a php script error but the code is the same of the main page (just for a test) and I have changed the include Route class path.

EDIT: no problem if you have a solution for Apache htaccess, I translate it :)

@steampixel
Copy link
Owner

Hi @matteovisotto
I will now create a nginx docker demo. So I can test your case. Unfortunately this could take a few weeks because I have not much time for this. Please be patient.

@steampixel
Copy link
Owner

For Apache your example works fine for me. Here is a tree of all included files. In my webroot I have created a "test" folder. Inside this I have created a "api" folder. Both folders and also the root folder have a .htaccess file and an index.php file. In this example I use the router three times in different dirs:

.
├── composer.json
├── docker
│   ├── image-php-7.2
│   │   └── Dockerfile
│   └── image-php-7.4.1
│       └── Dockerfile
├── docker-bash.sh
├── docker-build.sh
├── docker-rm.sh
├── docker-run.sh
├── include-example.php
├── index.php
├── LICENSE
├── README.md
├── release.sh
├── src
│   └── Steampixel
│       └── Route.php
├── test
│   ├── api
│   │   ├── composer.json
│   │   ├── composer.lock
│   │   ├── index.php
│   │   └── vendor
│   │       ├── autoload.php
│   │       ├── bin
│   │       ├── composer
│   │       │   ├── autoload_classmap.php
│   │       │   ├── autoload_namespaces.php
│   │       │   ├── autoload_psr4.php
│   │       │   ├── autoload_real.php
│   │       │   ├── autoload_static.php
│   │       │   ├── ClassLoader.php
│   │       │   ├── installed.json
│   │       │   ├── installed.php
│   │       │   ├── InstalledVersions.php
│   │       │   └── LICENSE
│   │       └── steampixel
│   │           └── simple-php-router
│   │               ├── composer.json
│   │               ├── docker
│   │               │   ├── image-php-7.2
│   │               │   │   └── Dockerfile
│   │               │   └── image-php-7.4.1
│   │               │       └── Dockerfile
│   │               ├── include-example.php
│   │               ├── index.php
│   │               ├── LICENSE
│   │               ├── README.md
│   │               ├── src
│   │               │   └── Steampixel
│   │               │       └── Route.php
│   │               └── web.config
│   ├── composer.json
│   ├── composer.lock
│   ├── index.php
│   └── vendor
│       ├── autoload.php
│       ├── bin
│       ├── composer
│       │   ├── autoload_classmap.php
│       │   ├── autoload_namespaces.php
│       │   ├── autoload_psr4.php
│       │   ├── autoload_real.php
│       │   ├── autoload_static.php
│       │   ├── ClassLoader.php
│       │   ├── installed.json
│       │   ├── installed.php
│       │   ├── InstalledVersions.php
│       │   └── LICENSE
│       └── steampixel
│           └── simple-php-router
│               ├── composer.json
│               ├── docker
│               │   ├── image-php-7.2
│               │   │   └── Dockerfile
│               │   └── image-php-7.4.1
│               │       └── Dockerfile
│               ├── include-example.php
│               ├── index.php
│               ├── LICENSE
│               ├── README.md
│               ├── src
│               │   └── Steampixel
│               │       └── Route.php
│               └── web.config
└── web.config

27 directories, 60 files

Here are some examples of the different rewrite bases inside the htaccess files:

RewriteBase /
RewriteBase /test/
RewriteBase /test/api/

The basepath inside each index.php will change in a similar way:

define('BASEPATH','/');
define('BASEPATH','/test/');
define('BASEPATH','/test/api/');

Everything works as expected in my Apache example. Hops that helps for now.

@matteovisotto
Copy link
Author

Thanks. I'll try again, also on Apache. (I can't at the moment but I'll let you know)

@steampixel
Copy link
Owner

Please share your config, if you're faster than me to get this to work on nginx.

@matteovisotto
Copy link
Author

matteovisotto commented Mar 16, 2021

I found the correct Nginx configuration assuming that the index.php file containing the routing instruction is contained in a subfolder called "test" and the "api" folder (with its index.php file) is a subfolder of test. The base location controls all the file in the main directory. It seems to be equals to the previous configuration but I created a new virtual host (maybe an old configuration was the problem)
`
server {

root /var/www/<main folder>;

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

server_name <domain name>;
    
    location / {
   #Delete this if the routing index.php is in the main directory 
    try_files $uri $uri/ =404;
    }

location /test/ {
	if (!-e $request_filename){
		rewrite ^(.*)$ /test/index.php;
	 }
}

   location /test/api/ {
	if (!-e $request_filename){
		rewrite ^(.*)$ /test/api/index.php;
	 }
   }

location ~ \.php$ {
	include snippets/fastcgi-php.conf;
#
#	# With php-fpm (or other unix sockets):
	fastcgi_pass unix:/run/php/php7.4-fpm.sock;
#	# With php-cgi (or other tcp sockets):
#	fastcgi_pass 127.0.0.1:9000;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
	deny all;
}

}
`

@steampixel
Copy link
Owner

Perfect! Thank you. I will use this maybe as a basis when creating the nginx docker example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants