The basics of php routing

0 votes

I'm seeking for a how-to guide or explanation of the most fundamental PHP routing.

For instance, I want to use the get function of a route class to supply the data when someone clicks on a URL like mywebsite.com/users, much like laravel does.

Route::get('users', function()
{
    return 'Users!';
});

Can somebody explain how to do this or provide me with some more information?

Jul 21, 2022 in PHP by narikkadan
• 63,600 points
655 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The web server handles routing for PHP in its most used configuration. The request path is mapped to a file to do this: The web server will really search for a file with the name test.php in a predefined directory if you type in www.example.org/test.php.

For our needs, the following functionality is useful: You can also call www.example.org/test.php/hello on many web servers, and test.php will still be executed. PHP uses the $_SERVER['PATH INFO'] variable to make the extra files in the requested path available. In this instance, it would have "/hello" in it.

You can build a very simple router like this:

<?php

// First, let's define our list of routes.
// We could put this in a different file and include it in order to separate
// logic and configuration.
$routes = array(
    '/'      => 'Welcome! This is the main page.',
    '/hello' => 'Hello, World!',
    '/users' => 'Users!'
);

// This is our router.
function router($routes)
{
    // Iterate through a given list of routes.
    foreach ($routes as $path => $content) {
        if ($path == $_SERVER['PATH_INFO']) {
            // If the path matches, display its contents and stop the router.
            echo $content;
            return;
        }
    }

    // This can only be reached if none of the routes matched the path.
    echo 'Sorry! Page not found';
}

// Execute the router with our list of routes.
router($routes);

?>

I hope this helps you.

answered Jul 23, 2022 by Kithuzzz
• 38,000 points

edited Mar 5

Related Questions In PHP

0 votes
1 answer

What is the use of $_REQUEST variable in php?

Hii @kartik, The $_REQUEST variable is used to read the ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,840 points
3,627 views
0 votes
1 answer

How can I handle the warning of file_get_contents() function in PHP?

Hello @kartik, This is fairly simple: if (!$data = ...READ MORE

answered Apr 7, 2020 in PHP by Niroj
• 82,840 points
11,450 views
0 votes
1 answer

What is the use of the @ symbol in PHP?

Hello @kartik, The @ symbol is the error control operator ("silence" or "shut-up" ...READ MORE

answered Apr 9, 2020 in PHP by Niroj
• 82,840 points
12,552 views
0 votes
1 answer

How to resolve the problem of losing a session after a redirect in PHP?

Hello @kartik, Carry out these usual checks: Make sure session_start(); is ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,840 points
35,714 views
0 votes
1 answer

Error:PHP Warning: POST Content-Length of 8978294 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

Hello @kartik, 8388608 bytes is 8M, the default ...READ MORE

answered Sep 17, 2020 in PHP by Niroj
• 82,840 points
29,062 views
0 votes
1 answer

How do I get the HTML code of a web page in PHP?

Hello @kartik, If your PHP server allows url ...READ MORE

answered Oct 6, 2020 in PHP by Niroj
• 82,840 points
9,724 views
0 votes
1 answer

Laravel 8 routes to controllers. SEO friendly URL structure

use regex to match the slugs  again use ...READ MORE

answered Feb 17, 2022 in Others by narikkadan
• 63,600 points
1,583 views
0 votes
1 answer

How to exclude specific folders every time I run 'eb deploy' using AWS EB CLI

If there is not .ebignore file it ...READ MORE

answered Oct 24, 2018 in AWS by Priyaj
• 58,020 points
2,245 views
0 votes
0 answers

Self Created Laravel Link Not Working

I installed laravel 6.0 through composer and ...READ MORE

Dec 3, 2019 in Others by anonymous
• 120 points
988 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP