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

PHP DIY series of custom configuration and routing

coldplay.xixi
Release: 2023-04-09 07:04:02
forward
2898 people have browsed it

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;,
    ],];
Copy after login

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);
    }}
Copy after login

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);
...省略代码
Copy after login

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);
    ...省略代码
Copy after login

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;);}
    ...省略代码
Copy after login
...省略代码
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);
    }
    ...省略代码
Copy after login

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,];
Copy after login

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
)
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template