bind() публичный статический Метод

设置路由绑定
public static bind ( mixed $bind, string $type = 'module' ) : mixed
$bind mixed 绑定信息
$type string 绑定类型 默认为module 支持 namespace class
Результат mixed
Пример #1
0
 public function testDomain()
 {
     $_SERVER['HTTP_HOST'] = 'subdomain.thinkphp.cn';
     $_SERVER['REQUEST_URI'] = '';
     Route::domain('subdomain.thinkphp.cn', 'sub?abc=test&status=1');
     Route::checkDomain();
     $this->assertEquals('sub?abc=test&status=1', Route::domain('subdomain.thinkphp.cn'));
     $this->assertEquals('sub', Route::bind('module'));
     $this->assertEquals('test', $_GET['abc']);
     $this->assertEquals(1, $_GET['status']);
     Route::domain('subdomain.thinkphp.cn', function () {
         return ['type' => 'module', 'module' => 'sub2'];
     });
     Route::checkDomain();
     $this->assertEquals('sub2', Route::bind('module'));
     Route::domain('subdomain.thinkphp.cn', '\\app\\index\\controller');
     Route::checkDomain();
     $this->assertEquals('\\app\\index\\controller', Route::bind('namespace'));
     Route::domain('subdomain.thinkphp.cn', '@\\app\\index\\controller\\blog');
     Route::checkDomain();
     $this->assertEquals('\\app\\index\\controller\\blog', Route::bind('class'));
     Route::domain('subdomain.thinkphp.cn', '[sub3]');
     Route::checkDomain();
     $this->assertEquals('sub3', Route::bind('group'));
 }
Пример #2
0
 public function testBind()
 {
     $request = Request::instance();
     Route::bind('index/blog');
     Route::get('index/blog/:id', 'index/blog/read');
     $result = Route::check($request, '10');
     $this->assertEquals(['index', 'blog', 'read'], $result['module']);
     Route::bind('\\app\\index\\controller', 'namespace');
     $this->assertEquals(['type' => 'method', 'method' => ['\\app\\index\\controller\\blog', 'read']], Route::check($request, 'blog/read'));
     Route::bind('\\app\\index\\controller\\blog', 'class');
     $this->assertEquals(['type' => 'method', 'method' => ['\\app\\index\\controller\\blog', 'read']], Route::check($request, 'read'));
 }
Пример #3
0
 /**
  * 执行应用程序
  * @access public
  * @param Request $request Request对象
  * @return Response
  * @throws Exception
  */
 public static function run(Request $request = null)
 {
     is_null($request) && ($request = Request::instance());
     try {
         $config = self::initCommon();
         if (defined('BIND_MODULE')) {
             // 模块/控制器绑定
             BIND_MODULE && Route::bind(BIND_MODULE);
         } elseif ($config['auto_bind_module']) {
             // 入口自动绑定
             $name = pathinfo($request->baseFile(), PATHINFO_FILENAME);
             if ($name && 'index' != $name && is_dir(APP_PATH . $name)) {
                 Route::bind($name);
             }
         }
         $request->filter($config['default_filter']);
         if ($config['lang_switch_on']) {
             // 开启多语言机制 检测当前语言
             Lang::detect();
         } else {
             // 读取默认语言
             Lang::range($config['default_lang']);
         }
         $request->langset(Lang::range());
         // 加载系统语言包
         Lang::load([THINK_PATH . 'lang' . DS . $request->langset() . EXT, APP_PATH . 'lang' . DS . $request->langset() . EXT]);
         // 获取应用调度信息
         $dispatch = self::$dispatch;
         if (empty($dispatch)) {
             // 进行URL路由检测
             $dispatch = self::routeCheck($request, $config);
         }
         // 记录当前调度信息
         $request->dispatch($dispatch);
         // 记录路由和请求信息
         if (self::$debug) {
             Log::record('[ ROUTE ] ' . var_export($dispatch, true), 'info');
             Log::record('[ HEADER ] ' . var_export($request->header(), true), 'info');
             Log::record('[ PARAM ] ' . var_export($request->param(), true), 'info');
         }
         // 监听app_begin
         Hook::listen('app_begin', $dispatch);
         switch ($dispatch['type']) {
             case 'redirect':
                 // 执行重定向跳转
                 $data = Response::create($dispatch['url'], 'redirect')->code($dispatch['status']);
                 break;
             case 'module':
                 // 模块/控制器/操作
                 $data = self::module($dispatch['module'], $config, isset($dispatch['convert']) ? $dispatch['convert'] : null);
                 break;
             case 'controller':
                 // 执行控制器操作
                 $data = Loader::action($dispatch['controller']);
                 break;
             case 'method':
                 // 执行回调方法
                 $data = self::invokeMethod($dispatch['method']);
                 break;
             case 'function':
                 // 执行闭包
                 $data = self::invokeFunction($dispatch['function']);
                 break;
             case 'response':
                 $data = $dispatch['response'];
                 break;
             default:
                 throw new \InvalidArgumentException('dispatch type not support');
         }
     } catch (HttpResponseException $exception) {
         $data = $exception->getResponse();
     }
     // 清空类的实例化
     Loader::clearInstance();
     // 输出数据到客户端
     if ($data instanceof Response) {
         $response = $data;
     } elseif (!is_null($data)) {
         // 默认自动识别响应输出类型
         $isAjax = $request->isAjax();
         $type = $isAjax ? Config::get('default_ajax_return') : Config::get('default_return_type');
         $response = Response::create($data, $type);
     } else {
         $response = Response::create();
     }
     // 监听app_end
     Hook::listen('app_end', $response);
     return $response;
 }