Manage URL routes in own php framework

前端 未结 12 2291
盖世英雄少女心
盖世英雄少女心 2021-02-02 16:34

I\'m creating a PHP Framework and I have some doubts...

The framework takes the url in this way: http:/web.com/site/index

It takes the first paramet

12条回答
  •  半阙折子戏
    2021-02-02 17:13

    You are missing a base path. The routing script must now where to start when detecting a pattern or route detected.

    Pseudo code:

     //set the base URI
    $base_uri = '/base';
    //remove the base URI from the original uri. You can also REGEX or string match against it if you want
    $route_uri = str_replace($base_uri,'',$uri);
    //perform route matching $route_uri, in your code a simple explode
    
    $url = explode('/',preg_replace('/^(\/)/','',$route_uri));
    

    You can use this with or without RewriteBase for your .htaccess so long as they use the same harness - index.php.

    Additionally, you can improve your route match procedure using Regular Expressions function like preg_match and preg_match_all. They let you define a pattern to match against and results to an array of matching strings - see http://php.net/manual/en/function.preg-match.php.

提交回复
热议问题