Home  >  Article  >  Backend Development  >  PHP DIY series of custom configuration and routing

PHP DIY series of custom configuration and routing

coldplay.xixi
coldplay.xixiforward
2020-07-07 16:53:162845browse

PHP DIY series of custom configuration and routing


We have developed it, but we need more. Such as custom configuration and routing.

Create new Config.php in the app folder

<?php/**
 *自定义配置
 */return [
    &#39;debug&#39; => false,
    &#39;route&#39; => [
        &#39;&#39; => &#39;demo/welcome&#39;,
        &#39;test&#39; => &#39;demo/test&#39;,
    ],];

Create newDemoController (under app/Https/Controllers directory)

<?php/**
 * Demo控制器
 */namespace App\Https\Controllers;use Library\Https\Controller;class DemoController extends Controller{
    public function welcome($params)
    {
        return $this->response->json([&#39;hello&#39; => &#39;welcome&#39;]);
    }

    public function test($params)
    {
        return $this->response->json($params);
    }}

Modify the entry file index.php and add the loading configuration code:

... 省略代码
// 加载配置
$config = require SF_LIBRARY_PATH.&#39;Config.php&#39;;
$appConfig = file_exists($appConfigPath = SF_APP_PATH.&#39;Config.php&#39;) ? require $appConfigPath : [];
$config = array_merge($config, $appConfig);
$config[&#39;debug&#39;] = ($config[&#39;debug&#39;]?? SF_DEBUG);
...省略代码

Also add custom routing processing to the parsing routing part:

// Application...省略代码
public function handleRequest(Request $request){
    $route = $request->resolve($this->_config[&#39;route&#39;]??[]);

    $response = $request->runAction($route);
    /**
     * 执行结果赋值给$response->data,并返回给response对象
     */
    if ($response instanceof Response) {
        return $response;
    }

    throw new SaiException(&#39;Content format error&#39;);}
    ...省略代码
    public function resolve($route=[])  {  
    $this->route = $route;  // 自定义路由  
    return $this->getPathUrl();  }
    // Request
    ...省略代码public function runAction($route){
    if (array_key_exists($route, $this->_route)) {
        $route = $this->_route[$route];
    }

    $match = explode(&#39;/&#39;, $route);
    $match = array_filter($match);
    ...省略代码

After saving, open the browser to see the effect:

PHP DIY series of custom configuration and routing

PHP DIY series of custom configuration and routing

#Although there are custom routes here, we sometimes need to disable the default route, so we might as well add the configuration parameter defaultRoute to control whether to enable the default route routing.

Let’s modify the routing parsing code:

//Application...省略代码
public function handleRequest(Request $request){
    $route = $request->resolve($this->_config[&#39;route&#39;]??[]);

    $response = $request->runAction($route, $this->_config[&#39;defaultRoute&#39;]??true);
    /**
     * 执行结果赋值给$response->data,并返回给response对象
     */
    if ($response instanceof Response) {
        return $response;
    }

    throw new SaiException(&#39;Content format error&#39;);}
    ...省略代码
...省略代码
public function runAction($route, $defaultRoute){
    if (array_key_exists($route, $this->_route)) {
        $route = $this->_route[$route];
    } elseif (!$defaultRoute) {
        throw new NotFoundException("route not found:".$route);
    }
    ...省略代码

In the Config under the app, add:

return [
    &#39;debug&#39; => false,
    &#39;route&#39; => [
        &#39;&#39; => &#39;demo/welcome&#39;,
        &#39;test&#39; => &#39;demo/test&#39;,
    ],
    &#39;defaultRoute&#39; => false,];

We open the browser and enter saif.com/login

The error is reported as follows:

Array
(
    [line] => 137
    [msg] => route not found:login
    [code] => 404
    [file] => library/Https/Request.php
)

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of PHP DIY series of custom configuration and routing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete