douhan9748 2014-01-06 22:57
浏览 30
已采纳

php路由的基础知识

I'm looking for a tutorial or explaination on how to do very basic php routing.

For example when I visit a link like: mywebsite.com/users I want to run the get method of a route class to provide the data, in the same way laravel does it.

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

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

  • 写回答

3条回答 默认 最新

  • doubi2228 2014-01-06 23:32
    关注

    In its most common configuration, PHP relies on the web server to do the routing. This is done by mapping the request path to a file: If you request www.example.org/test.php, the web server will actually look for a file named test.php in a pre-defined directory.

    There is a feature that comes in handy for our purpose: Many web servers also allow you to call www.example.org/test.php/hello and it will still execute test.php. PHP makes the additional stuff in the requested path accessible via the $_SERVER['PATH_INFO'] variable. In this case it would contain "/hello".

    Using this, we 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);
    
    ?>
    

    For the sake of simplicity, I did not make the router a class. But from here on, that shouldn't be a problem either.

    Let's assume we named this file index.php. We can now call www.example.org/index.php/hello to get a nice "Hello, World!" message. Or www.example.org/index.php/ to get the main page.

    The "index.php" in that URL is still ugly, but we can fix this by using URL rewriting. In Apache HTTPD you would put a .htaccess file in the same directory with the following content:

    RewriteEngine on
    RewriteRule ^(.*)$ index.php/$1
    

    And there you are! Your very own router with under 10 lines of logic code (not counting comments and the routes list).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
  • ¥15 在启动roslaunch时出现如下问题
  • ¥15 汇编语言实现加减法计算器的功能
  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?