steampixel / simplePHPRouter

This is a simple and small single class PHP router that can handel the whole url routing for your project.
MIT License
406 stars 117 forks source link

excluding a directory from using the route? #67

Open unikoca opened 1 year ago

unikoca commented 1 year ago

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,

ibnsultan commented 1 year ago

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

unikoca commented 1 year ago

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

steampixel commented 1 year ago

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?

unikoca commented 10 months ago

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.

ibnsultan commented 10 months ago

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

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

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