{"id":935,"date":"2014-10-14T15:33:49","date_gmt":"2014-10-14T07:33:49","guid":{"rendered":"http:\/\/blog.turn.tw\/?page_id=935"},"modified":"2014-10-15T11:59:24","modified_gmt":"2014-10-15T03:59:24","slug":"%e8%ab%8b%e5%be%80%e5%88%a5%e8%99%95%ef%bc%9aroutingredirector-php","status":"publish","type":"page","link":"https:\/\/blog.turn.tw\/?page_id=935","title":{"rendered":"\u8acb\u5f80\u5225\u8655\uff1aRouting\/Redirector.php"},"content":{"rendered":"<p>\u5728\u5b98\u65b9\u6587\u4ef6\uff1a<a href=\"http:\/\/laravel.com\/docs\/4.2\/responses\">Views &#038; Responses<\/a>\u88e1\u547c\u53ebRedirect\u985e\u5225\u975c\u614b\u51fd\u5f0f\uff0c\u80cc\u5f8c\u662f\u5728\u547c\u53eb\u9019\u652fRedirector.php\u985e\u5225\u7684\u5be6\u9ad4\u3002<br \/>\n\u4e3b\u8981\u529f\u80fd\u662f\u7531UrlGenerator\u985e\u5225\u7522\u751fURL\u3001\u518d\u4e1f\u7d66RedirectResponse\u985e\u5225\u7522\u751f\u8f49\u5740\u7684HTTP header\u3002<br \/>\n\u6b64\u5916Redirector\u9084\u63d0\u4f9b\u67d0\u4e9b\u9700\u8981\u7d00\u9304\u7576\u524dURL\u3001\u4e0a\u4e00\u500bURL\u76f8\u95dc\u7684\u8f49\u5740\u529f\u80fd\uff0c\u6240\u4ee5\u9084\u9700\u8981Session\u7684\u5354\u52a9\u3002<br \/>\n<red><br \/>\n\u6b64\u8655\u63d0\u5230\u7684\u8a31\u591a\u51fd\u5f0f\uff0c\u5b98\u7db2\u90fd\u6c92\u6709\u5beb\u51fa\u4f86\u5594\uff01<br \/>\n<\/red><\/p>\n<pre>\r\n<?php namespace Illuminate\\Routing;\r\n\r\nuse Illuminate\\Http\\RedirectResponse;\r\nuse Illuminate\\Session\\Store as SessionStore;\r\n\r\nclass Redirector {\r\n\r\n\t\/**\r\n\t * The URL generator instance.\r\n\t *\r\n\t * @var \\Illuminate\\Routing\\UrlGenerator\r\n\t *\/\r\n\tprotected $generator;\r\n\r\n\t\/**\r\n\t * The session store instance.\r\n\t *\r\n\t * @var \\Illuminate\\Session\\Store\r\n\t *\/\r\n\tprotected $session;\r\n\r\n\t\/**\r\n\t * Create a new Redirector instance.\r\n\t *\r\n\t * @param  \\Illuminate\\Routing\\UrlGenerator  $generator\r\n\t * @return void\r\n\t *\/\r\n\tpublic function __construct(UrlGenerator $generator)\r\n\t{\r\n\t\t$this->generator = $generator;\r\n\t}\r\n\r\n\t\/**\r\n\t * Create a new redirect response to the \"home\" route.\r\n\t *\r\n\t * @param  int  $status\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tpublic function home($status = 302)\r\n\t{\r\n\t\treturn $this->to($this->generator->route('home'), $status);\r\n\t}\r\n<\/pre>\n<p>\u5982\u679c\u6709\u8a2d\u5b9a\u540d\u70ba&#8217;home&#8217;\u7684Named Route\uff0c\u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u76f4\u63a5\u8f49\u5f80\u5b83\u3002<br \/>\n\u6240\u4ee5Laravel\u5176\u5be6\u9f13\u52f5\u4f60\u7684routes\u4e2d\u8a2d\u4e00\u500b\u540d\u70bahome\u7684\u9996\u9801\u5462\u3002<\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new redirect response to the previous location.\r\n\t *\r\n\t * @param  int    $status\r\n\t * @param  array  $headers\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tpublic function back($status = 302, $headers = array())\r\n\t{\r\n\t\t$back = $this->generator->getRequest()->headers->get('referer');\r\n\r\n\t\treturn $this->createRedirect($back, $status, $headers);\r\n\t}\r\n<\/pre>\n<p>\u5f9eHTTP HEADER\u7684REFERER\u627e\u5230\u524d\u4e00\u500b\u7db2\u5740\uff0c\u7136\u5f8c\u8f49\u904e\u53bb\u3002<\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new redirect response to the current URI.\r\n\t *\r\n\t * @param  int    $status\r\n\t * @param  array  $headers\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tpublic function refresh($status = 302, $headers = array())\r\n\t{\r\n\t\treturn $this->to($this->generator->getRequest()->path(), $status, $headers);\r\n\t}\r\n<\/pre>\n<p>\u8f49\u5f80\u539f\u7db2\u5740\u3002<br \/>\n<red><br \/>\n\u54a6\uff1f\uff1f\uff1f\u4ec0\u9ebc\u60c5\u6cc1\u6703\u9700\u8981\u539f\u5730\u8f49\u5740\uff1f\uff1f\uff1f<br \/>\n<\/red><\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new redirect response, while putting the current URL in the session.\r\n\t *\r\n\t * @param  string  $path\r\n\t * @param  int     $status\r\n\t * @param  array   $headers\r\n\t * @param  bool    $secure\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tpublic function guest($path, $status = 302, $headers = array(), $secure = null)\r\n\t{\r\n\t\t$this->session->put('url.intended', $this->generator->full());\r\n\r\n\t\treturn $this->to($path, $status, $headers, $secure);\r\n\t}\r\n<\/pre>\n<p>\u8f49\u5740\u7684\u540c\u6642\u8a18\u4e0b\u7576\u524d\u7684URL\u3002<\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new redirect response to the previously intended location.\r\n\t *\r\n\t * @param  string  $default\r\n\t * @param  int     $status\r\n\t * @param  array   $headers\r\n\t * @param  bool    $secure\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tpublic function intended($default = '\/', $status = 302, $headers = array(), $secure = null)\r\n\t{\r\n\t\t$path = $this->session->pull('url.intended', $default);\r\n\r\n\t\treturn $this->to($path, $status, $headers, $secure);\r\n\t}\r\n<\/pre>\n<p>\u8f49\u5f80\u4e0a\u4e00\u6b21guest\u51fd\u5f0f\u57f7\u884c\u6642\u8a18\u4e0b\u7684URL\u3002<\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new redirect response to the given path.\r\n\t *\r\n\t * @param  string  $path\r\n\t * @param  int     $status\r\n\t * @param  array   $headers\r\n\t * @param  bool    $secure\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tpublic function to($path, $status = 302, $headers = array(), $secure = null)\r\n\t{\r\n\t\t$path = $this->generator->to($path, array(), $secure);\r\n\r\n\t\treturn $this->createRedirect($path, $status, $headers);\r\n\t}\r\n<\/pre>\n<p>\u8f49\u5f80\u6307\u5b9aURL\u3002<br \/>\n\u80fd\u63a5\u53d7$secure\u53c3\u6578\uff0c\u5224\u65b7\u662f\u5426\u70baHTTPS\u5354\u5b9a\uff0c\u4fee\u6539URL\u4e4b\u5f8c\u624d\u8f49\u3002<\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new redirect response to an external URL (no validation).\r\n\t *\r\n\t * @param  string  $path\r\n\t * @param  int     $status\r\n\t * @param  array   $headers\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tpublic function away($path, $status = 302, $headers = array())\r\n\t{\r\n\t\treturn $this->createRedirect($path, $status, $headers);\r\n\t}\r\n<\/pre>\n<p>\u8f49\u5f80\u5916\u90e8\u7db2\u5740\u3002<br \/>\n<red><br \/>\n\u5176\u5be6to\u51fd\u5f0f\u4e5f\u53ef\u4ee5\u8f49\u5f80\u5916\u90e8\u7db2\u5740\uff0c\u9019\u500baway\u51fd\u5f0f\u76ee\u524d\u6c92\u6709\u610f\u7fa9\u3002<br \/>\n\u61c9\u8a72\u662f\u653e\u5728\u9019\u908a\uff0c\u63d0\u9192\u4e4b\u5f8c\u8981\u5be6\u4f5c\u6b64\u529f\u80fd\u5427\u3002<br \/>\n<\/red><\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new redirect response to the given HTTPS path.\r\n\t *\r\n\t * @param  string  $path\r\n\t * @param  int     $status\r\n\t * @param  array   $headers\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tpublic function secure($path, $status = 302, $headers = array())\r\n\t{\r\n\t\treturn $this->to($path, $status, $headers, true);\r\n\t}\r\n<\/pre>\n<p>\u8f49\u5f80\u67d0HTTPS\u5354\u5b9a\u7684\u7db2\u5740\u3002<br \/>\n<red><br \/>\n\u610f\u7fa9\u4e0d\u5927\uff0c\u4f7f\u7528to\u51fd\u5f0f\u4e26\u5c07\u7b2c\u56db\u500b\u53c3\u6578\u8a2d\u70batrue\u5373\u53ef\u3002<br \/>\n<\/red><\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new redirect response to a named route.\r\n\t *\r\n\t * @param  string  $route\r\n\t * @param  array   $parameters\r\n\t * @param  int     $status\r\n\t * @param  array   $headers\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tpublic function route($route, $parameters = array(), $status = 302, $headers = array())\r\n\t{\r\n\t\t$path = $this->generator->route($route, $parameters);\r\n\r\n\t\treturn $this->to($path, $status, $headers);\r\n\t}\r\n<\/pre>\n<p>routes\u5982\u679c\u6709\u547d\u540d\u7684\u8a71\uff0c\u53ef\u4ee5\u7528\u6b64\u51fd\u5f0f\u8f49\u5f80\u7279\u5b9aname\u3002<\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new redirect response to a controller action.\r\n\t *\r\n\t * @param  string  $action\r\n\t * @param  array   $parameters\r\n\t * @param  int     $status\r\n\t * @param  array   $headers\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tpublic function action($action, $parameters = array(), $status = 302, $headers = array())\r\n\t{\r\n\t\t$path = $this->generator->action($action, $parameters);\r\n\r\n\t\treturn $this->to($path, $status, $headers);\r\n\t}\r\n<\/pre>\n<p>\u8f49\u5f80\u67d0\u500bcontroller action\u3002\u5c31\u662f\u985e\u4f3c&#8217;UserController@showProfile&#8217;\u7684\u6771\u897f\u3002<\/p>\n<pre>\r\n\t\/**\r\n\t * Create a new redirect response.\r\n\t *\r\n\t * @param  string  $path\r\n\t * @param  int     $status\r\n\t * @param  array   $headers\r\n\t * @return \\Illuminate\\Http\\RedirectResponse\r\n\t *\/\r\n\tprotected function createRedirect($path, $status, $headers)\r\n\t{\r\n\t\t$redirect = new RedirectResponse($path, $status, $headers);\r\n\r\n\t\tif (isset($this->session))\r\n\t\t{\r\n\t\t\t$redirect->setSession($this->session);\r\n\t\t}\r\n\r\n\t\t$redirect->setRequest($this->generator->getRequest());\r\n\r\n\t\treturn $redirect;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the URL generator instance.\r\n\t *\r\n\t * @return  \\Illuminate\\Routing\\UrlGenerator\r\n\t *\/\r\n\tpublic function getUrlGenerator()\r\n\t{\r\n\t\treturn $this->generator;\r\n\t}\r\n\r\n\t\/**\r\n\t * Set the active session store.\r\n\t *\r\n\t * @param  \\Illuminate\\Session\\Store  $session\r\n\t * @return void\r\n\t *\/\r\n\tpublic function setSession(SessionStore $session)\r\n\t{\r\n\t\t$this->session = $session;\r\n\t}\r\n\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5b98\u65b9\u6587\u4ef6\uff1aViews &#038; Responses\u88e1\u547c\u53ebRedirect\u985e\u5225\u975c\u614b\u51fd\u5f0f\uff0c\u80cc\u5f8c\u662f\u5728\u547c\u53eb\u9019\u652fR &hellip; <a href=\"https:\/\/blog.turn.tw\/?page_id=935\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">\u8acb\u5f80\u5225\u8655\uff1aRouting\/Redirector.php<\/span> <span class=\"meta-nav\">&rarr;<\/span> <\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_mi_skip_tracking":false},"_links":{"self":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/935"}],"collection":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=935"}],"version-history":[{"count":4,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/935\/revisions"}],"predecessor-version":[{"id":941,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/935\/revisions\/941"}],"wp:attachment":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}