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

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编程从入门到精通

更多相关文章

  1. 实现简单的php购物车代码
  2. PHP解析XML的几种方法(附代码)
  3. php之接口与前端数据交互实现示例代码
  4. PHP如何解压缩zip文件?(代码示例)
  5. PHP如何实现AES加密、解密?方法介绍(代码示例)
  6. 示例PHP MemCached高级缓存应用代码
  7. PHP实现代码复用的traits新特性的方法
  8. 详解PHP论坛实现积分系统的思路代码
  9. 详解PHP网页缓存技术优点及代码实例

随机推荐

  1. 工作中傻傻的错-2011/11
  2. 使用字符串参数调用AndroidJni静态方法。
  3. 在jasper中获取组部分报告API java
  4. 抢分啦!谁能帮我解决java的Socket的问题?
  5. JNI学习笔记(五)——fields和methods
  6. Java常量表达式相关的编译优化代码
  7. 请问JAVA求职英语水平的要求
  8. Java NIO 学习笔记(七)----NIO/IO 的对比和
  9. Java: SAX解析一个巨大的XML文件。
  10. 使用JNA从java崩溃VM调用c,有人能告诉我为