taniarascia / comments

Comments
7 stars 0 forks source link

the-simplest-php-router/ #26

Open utterances-bot opened 4 years ago

utterances-bot commented 4 years ago

The Simplest PHP Router | Tania Rascia

I wanted to create the absolute most basic routing code in PHP, so here it is. We will direct ALL traffic to and route to the new files…

https://www.taniarascia.com/the-simplest-php-router/

nielsoffice commented 4 years ago

Hello, this is very simple but not working did you test that ?

TilP commented 4 years ago

2+ years later, first time seeing this. I am getting back into PHP after so many years away. Just wanted to mention I think you can make your code even shorter by having the first two cases represent loading of index.php as:

case '': case '/': // code, then break

Another way is to use switch(true)... still combining the two expressions on one line. Cheers

nielsoffice commented 4 years ago

Hello! Thanks.

On Sun, Sep 20, 2020, 3:02 AM Attilio P. notifications@github.com wrote:

2+ years later, first time seeing this. I am getting back into PHP after so many years away. Just wanted to mention I think you can make your code even shorter by having the first two cases represent loading of index.php as:

case '': case '/': // code, then break

Another way is to use switch(true)... still combining the two expressions on one line. Cheers

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/taniarascia/comments/issues/26#issuecomment-695344977, or unsubscribe https://github.com/notifications/unsubscribe-auth/AQCIWAMYA2UM3RPG6TYJAD3SGT52VANCNFSM4QG4OSQQ .

ghost commented 4 years ago

Thank you so much Rascia! ❤

teresko commented 3 years ago

Why the hell are you referring to HTML fragments as "views"?

hamdygamal commented 3 years ago

but this could not be used like MVC

GovindRajeesh commented 3 years ago

/ path not working.

ridhwan102 commented 2 years ago

Hi, I manage to make this simple routing work by changing few line on htaccess and add some str_replace on index.php. But somehow I'm not able to use action in form. Can you help me?

mfoland90 commented 2 years ago

The / doesn't work.. If you do something in a sub directory, in my case, I have it in /dev (since I'm building an app), you have to put the /dev on the router. Looks like it might need some different part of the URI... The error part worked nonetheless haha. I'm struggling with this one. Can you help?

mfoland90 commented 2 years ago

You might be able to put the path. When I was testing a theory, i'd do the whole path, especially on the app I built, and am rebuilding with routing.. i'd do uri/idea/ideaname or with something like a post it would be action="?mode=Create"

mfoland90 commented 2 years ago

Actually... For all saying the / path isn't working, etc.. I did some playing around with this... https://github.com/mfoland90/simple-php-router/blob/main/Route.php

Best wishes :)

mfoland90 commented 2 years ago

Even my work around ended up not working for me.. My project is in a subfolder, so the path isn't working. It may on a subdomain, but on a subdolder, nope.

mfoland90 commented 2 years ago

Another issue is if you are doing something with a page title or something in the uri is it's not dynamic.

MukhtaarAziz commented 2 years ago

Thanks Tania. It is very simple, We also can extending it using $_SERVER['REQUEST_METHOD'] to control over HTTP method type. anyway many thanks to you.

skshailendra151 commented 2 years ago

It's not working while I am using parameter in the URL like" www.abc.one//?utm_source=QRCode&utm_medium=Card" please suggest me what can I do ?

mfoland90 commented 2 years ago

This is what I did.. https://github.com/steampixel/simplePHPRoute It was simply easy to install using composer :) I built a Request System and it uses this.

On Wed, Dec 22, 2021 at 12:57 AM skshailendra151 @.***> wrote:

It's not working while I am using parameter in the URL like" www.abc.one//?utm_source=QRCode&utm_medium=Card" please suggest me what can I do ?

— Reply to this email directly, view it on GitHub https://github.com/taniarascia/comments/issues/26#issuecomment-999307688, or unsubscribe https://github.com/notifications/unsubscribe-auth/AW34SFGMREQU46GGEPHGYDDUSFSFFANCNFSM4QG4OSQQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

NetoBraghetto commented 2 years ago

I would do something like this:

$urlpath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
switch ($urlpath) {
...

to support querystring

mfoland90 commented 2 years ago

I found another one like steampixel router class. It did what I needed lol.

On Mon, Jan 31, 2022, 8:03 AM Neto Braghetto @.***> wrote:

I would do something like this:

$urlpath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);switch ($urlpath) { ...

to support querystring

— Reply to this email directly, view it on GitHub https://github.com/taniarascia/comments/issues/26#issuecomment-1025716228, or unsubscribe https://github.com/notifications/unsubscribe-auth/AW34SFDF4JSBZ4FAFKWNG3LUY2CDDANCNFSM4QG4OSQQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

iPhoneSDKPro commented 2 years ago

This is my idea of php routing. RoutREST https://github.com/iPhoneSDKPro/routeREST

rajibbrunel commented 2 years ago

Thank so much for this smart solution. with love and respect.

gabriel-ands commented 2 years ago

Maybe you need modify the last line .htaccess for

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [NC,L] ### alternative

venusdemilo commented 2 years ago

To retrieve a variable in the url, for example in GET mode : "http://mysite.com/?action=foo&id=14 " you just have to do in the router :

case '/?action=foo&id='.$_GET['id'] :
    ... PHP tricks with the variable $_GET['id'] ...
    break; 

you can also retrieve the content of a form sent in POST mode:

case '/?action=authentication' :
        ... stuff in PHP with the variable $_POST ...
        break;

It works very well in an MVC system ...

nigamanthsrivatsan commented 1 year ago

wow, this is some crazy shit fr

Colandus commented 1 year ago

Works fine, thank you.

Colandus commented 1 year ago

However, to support GET-parameters, you could use this:

$request = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

jmartincc commented 1 year ago

Works great! But I had to do some googling for NGINX. I suggest you add that to help others!

In my Nginix website conf file I added:

location / {
                try_files $uri $uri/ /index.php?$args; #if doesn't exist, send it to index.php
}

Then followed the rest of your guide normally.

Thanks!

jmartincc commented 1 year ago

Also for PHP 8.x, I used match instead of switch and it works like a charm.

match ($request) {
        '/test' => test(),
        default => require __DIR__ . '/error.php';,
    };
ChaosWebDev commented 1 year ago

How would you do this if you have it multiple deep?

e.g.: /page_one/page_two

andrew-lim commented 1 year ago

To make it work for any sub folder and query strings:

function getBaseURI()
{
    $basePath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
    // Get the current Request URI and remove rewrite base path from it
    // (= allows one to run the router in a sub folder)
    $uri = substr(rawurldecode($_SERVER['REQUEST_URI']), strlen($basePath));
    // Don't take query params into account on the URL
    if (strstr($uri, '?')) {
        $uri = substr($uri, 0, strpos($uri, '?'));
    }
    // Remove trailing slash + enforce a slash at the start
    return '/' . trim($uri, '/');
}

$request = getBaseURI();
jorwan commented 1 year ago

Thanks, I would add !/ in RewriteRule to avoid a infinity redirect loop RewriteRule !/ index.php [QSA,L]