simplePHPRouter icon indicating copy to clipboard operation
simplePHPRouter copied to clipboard

excluding a directory from using the route?

Open unikoca opened this issue 2 years ago • 5 comments

Hi,

On a particular project, i'm using your library on the root of the domain, everything works fine. But now, they install a folder ( ex: /abc/ ) that doesn't need to be managed by your routing. Is there a way to 'exclude' a folder from your routing?

Regards,

unikoca avatar Nov 28 '22 15:11 unikoca

From what I know traditional routes should also work fine, let say you have file feature.php in folder abc

domain.com/abc/feature.php should also work

ibnsultan avatar Nov 28 '22 15:11 ibnsultan

It's doesn't seems to work in my case, since everything is passing thru index.php (router) already, loading config, etc... within the htaccess file. Some conflics occurs.

i may put the entire routing in a if/else statement but it's not very clean... Better ways are welcome. 👍

unikoca avatar Nov 28 '22 16:11 unikoca

Requests to existing files and folders should be delivered directly without going through the index.php. There are rules to achieve this inside the .htaccess:

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d

You can test this rules: Place a file (test.txt) into your root folder and try to call it: https://mydomain.com/test.txt You should then see its contents. Also you can place such a file into your sub folder and then call https://mydomain.com/abc/test.txt Both request should bypass the router. If this does not work you should check your .htaccess file. Maybe it was modified?

steampixel avatar Nov 29 '22 08:11 steampixel

I think I wasn't clear enough... I'm not talking about accessing a particular file within the /abc/ folder.

  1. let's say i use the regex '/([a-z0-9-/]*)' on the root of my applicaiton. It will grab /asd/sss/... and so on.
  2. now I want to 'exclude' a particular path that could be included in the first regex. (ex: /admin/...). I don't want the use a routing at all for this path.

note that the first regex will glab all the possible paths & files on the root and the subfolders. It is useful when you create folders from an admin panel for example.

could be useful to have an 'exclude' method to remove a file/path from the possibilities.

unikoca avatar Jan 11 '24 16:01 unikoca

3 ways you can achieve that @unikoca

  1. use of regexp: ^(?!.(?:admin|other_word|yet_another)).[a-zA-Z0-9-/]+.*$ (Recommended)
Route::add('/(^(?!.*(?:admin|other_word|yet_another)).*[a-zA-Z0-9-/]+.*$)', function() {
  return 'admin and some words not allowed';
});

You can even go further to define which words to begin with in your route

  1. add another parameter function of type array or string on the add function, and handle any words or expressions you don't want on the specific route

  2. If you're using Apache web server, use .htaccess to set custom rules

ibnsultan avatar Jan 16 '24 04:01 ibnsultan