Home > Article > Backend Development > 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 [ 'debug' => false, 'route' => [ '' => 'demo/welcome', 'test' => 'demo/test', ],];
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(['hello' => 'welcome']); } 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.'Config.php'; $appConfig = file_exists($appConfigPath = SF_APP_PATH.'Config.php') ? require $appConfigPath : []; $config = array_merge($config, $appConfig); $config['debug'] = ($config['debug']?? SF_DEBUG); ...省略代码
Also add custom routing processing to the parsing routing part:
// Application...省略代码 public function handleRequest(Request $request){ $route = $request->resolve($this->_config['route']??[]); $response = $request->runAction($route); /** * 执行结果赋值给$response->data,并返回给response对象 */ if ($response instanceof Response) { return $response; } throw new SaiException('Content format error');} ...省略代码 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('/', $route); $match = array_filter($match); ...省略代码
After saving, open the browser to see the effect:
#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['route']??[]); $response = $request->runAction($route, $this->_config['defaultRoute']??true); /** * 执行结果赋值给$response->data,并返回给response对象 */ if ($response instanceof Response) { return $response; } throw new SaiException('Content format error');} ...省略代码
...省略代码 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 [ 'debug' => false, 'route' => [ '' => 'demo/welcome', 'test' => 'demo/test', ], 'defaultRoute' => 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!