百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>网页制作> PHP DIY系列之自定义配置和路由
分享文章到:

PHP DIY系列之自定义配置和路由

发布时间:09/01 来源:未知 浏览: 关键词:


我们已经开发完成,但我们还需要更多。比方自定义配置和路由。

app文件夹下创建Config.php

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

创建DemoController(app/Https/Controllers名目下)

<?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);
    }}

修改入口文件index.php,参加加载配置代码:

... 省略代码
// 加载配置
$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);
...省略代码

解析路由部分也参加自定义路由处置:

// 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);
    ...省略代码

留存后翻开阅读器看看结果:

image

image

这里虽然有自定义路由,但是我们有时候需要制止默许路由,所以我们无妨增添配置参数defaultRoute,用来操纵可否开启默许路由。

我们修改一下路由解析的代码:

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

我们在app下面的Config,参加:

return [
    'debug' => false,
    'route' => [
        '' => 'demo/welcome',
        'test' => 'demo/test',
    ],
    'defaultRoute' => false,];

我们翻开阅读器输入saif.com/login

报错如下:

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

相关学习引荐:PHP编程从入门到熟知

以上就是PHP DIY系列之自定义配置和路由的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板