public function register() {
// 注册路由管理,提供路由注册,路由匹配的功能
$this->registerRouter();
// 注册 Url 生成器实例
$this->registerUrlGenerator();
// 注册跳转器
$this->registerRedirector();
// 绑定 PSR-7 请求实现到 ServerRequestInterface 接口
$this->registerPsrRequest();
// 绑定 PSR-7 Response 实现到 ResponseInterface 接口
$this->registerPsrResponse();
// 注册 ReponseFactory,提供各式各样的 Response,比如视图响应、Json响应、Jsonp响应、文件下载等
$this->registerResponseFactory();
}
public function map(Router $router){
require __DIR__.'/routes.php';
}
$this->router->dispatch($request)
public function dispatch(Request $request) {
$this->currentRequest = $request;
return $this->dispatchToRoute($request);
}
public function dispatchToRoute(Request $request) {
// 根据请求的 url 找到匹配的路由
$route = $this->findRoute($request);
// 将路由绑定到请求上
$request->setRouteResolver(function () use ($route) {
return $route;
}
// 触发 RouteMatched 事件
$this->events->dispatch(new Events\RouteMatched($route, $request));
// 通过 Pipeline 流水线执行路由上绑定的中间件及对应的方法
$response = $this->runRouteWithinStack($route, $request);
// 根据 request 请求设置 response 的响应头
return $this->prepareResponse($request, $response);
}
$url = new UrlGenerator(
$routes = new RouteCollection,
$request = Request::create('http://www.foo.com/')
);
$url->to('foo/bar'); // 输出 http://www.foo.com/foo/bar
// 路径名是 foo/bar,当前请求的根路径为 http://www.foo.com,所以输出是 http://www.foo.com/foo/bar
$url->to('foo/bar')
// 路径名是 foo/bar,当前请求的根路径为 http://www.foo.com,第三个参数决定 scheme 是 https,所以输出是 https://www.foo.com/foo/bar
$url->to('foo/bar', [], true)
// 路径名是 foo/bar,第二个参数 是补充路径名,implode 后是 /baz/boom
// 第三个参数决定 scheme 是 https,所以输出是 https://www.foo.com/foo/bar/baz/boom
$url->to('foo/bar', ['baz', 'boom'], true)
// 路径名是 foo/bar,查询参数是 ?foo=bar ,补充路径是 /baz,所以输出是 https://www.foo.com/foo/bar/baz?foo=bar
$url->to('foo/bar?foo=bar', ['baz'], true)
$route = new Route(['GET'], 'foo/bar', ['as' => 'foo']);
$routes->add($route);
// 输出 'http://www.foo.com/foo/bar
$url->route('foo');
// 第三个参数为 false,表示不显示根目录,于是输出 /foo/bar
$url->route('foo', [], false)
// 路由中的 url 本身不带参数,则第二参数中所有关联数组都将作为查询参数
// 输出 /foo/bar?foo=bar
$url->route('foo', ['foo' => 'bar'], false)
$route = new Route(['GET'], 'foo/bar/{baz}/breeze/{boom}', ['as' => 'bar']);
$routes->add($route);
// 路由上的 url 带参数,根据参数名找值;剩余多余的为查询参数;
// 输出 http://www.foo.com/foo/bar/otwell/breeze/taylor?fly=wall
$url->route('bar', ['boom' => 'taylor', 'baz' => 'otwell', 'fly' => 'wall']);
// 路由上的 url 带参数,找不到对应的参数值,则按顺序作值;剩余多余的为查询参数;
// 输出 http://www.foo.com/foo/bar/taylor/breeze/otwell?fly=wall
$url->route('bar', ['taylor', 'otwell', 'fly' => 'wall']);
$route = new Route(['GET'], 'foo/bam', ['controller' => 'foo@bar']);
$routes->add($route);
// 输出 http://www.foo.com/foo/bam
$url->action('foo@bar');
$route = new Route(['GET'], 'foo/invoke', ['controller' => 'InvokableActionStub']);
$routes->add($route);
// 输出 http://www.foo.com/foo/invoke
$url->action('InvokableActionStub');
$url->defaults(['locale' => 'en']);
$route = new Route(['GET'], 'foo', ['as' => 'defaults', 'domain' => '{locale}.example.com', function() {}]);
// 路由 url 有参数,但没有传参数值,则会找全局默认参数值;输出 http://en.example.com/foo
$url->route('defaults');
// 设置全局命名空间
$url->setRootControllerNamespace('namespace');
// 配置添加路由
$route = new Route(['GET'], 'foo/bar', ['controller' => 'namespace\foo@bar']);
$routes->add($route);
$route = new Route(['GET'], 'foo/invoke', ['controller' => 'namespace\InvokableActionStub']);
$routes->add($route);
// 输出 http://www.foo.com/foo/bar; action 的值省略 namespace 这个命名空间
$url->action('foo@bar');
// 输出 http://www.foo.com/foo/invoke; action 的值省略 namespace 这个命名空间
$url->action('InvokableActionStub');
// 配置添加路由
$route = new Route(['GET'], 'something/else', ['controller' => 'something\foo@bar']);
$routes->add($route);
// 输出 http://www.foo.com/something/else; action 的最前面加了 `\`,全局命名空间下调用
$url->action('\something\foo@bar');
public function home($status = 302)
public function back($status = 302, $headers = [], $fallback = false)
if ($url) {
return $url;
} elseif ($fallback) {
return $this->to($fallback);
} else {
return $this->to('/');
}
public function refresh($status = 302, $headers = [])
public function to($path, $status = 302, $headers = [], $secure = null)
public function away($path, $status = 302, $headers = [])
public function secure($path, $status = 302, $headers = [])
return $this->to($path, $status, $headers, true);
public function route($route, $parameters = [], $status = 302, $headers = [])
public function action($action, $parameters = [], $status = 302, $headers = [])
public function guest($path, $status = 302, $headers = [], $secure = null)
public function intended($default = '/', $status = 302, $headers = [], $secure = null)
response()->view('hello', $data, 200);
response()->json(['name' => 'Abigail', 'state' => 'CA']);
response()->json(['name' => 'Abigail', 'state' => 'CA'])->withCallback($request->input('callback'));
response()->file($pathToFile, $headers);
return response()->download($pathToFile, $name, $headers);
| 方法名 | 调用 | 实际调用的是跳转器中的哪个方法 |
|---|---|---|
| redirectTo | response()->redirectTo(...) | to方法 |
| redirectToRoute | response()->redirectToRoute(...) | route方法 |
| redirectToAction | response()->redirectToAction(...) | action方法 |
| redirectGuest | response()->redirectGuest(...) | guest方法 |
| redirectToIntended | response()->redirectToIntended(...) | intended方法 |
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有