{"id":942,"date":"2014-10-15T12:10:24","date_gmt":"2014-10-15T04:10:24","guid":{"rendered":"http:\/\/blog.turn.tw\/?page_id=942"},"modified":"2014-10-15T12:23:44","modified_gmt":"2014-10-15T04:23:44","slug":"mvc-controller%ef%bc%9aroutingcontroller-php","status":"publish","type":"page","link":"https:\/\/blog.turn.tw\/?page_id=942","title":{"rendered":"MVC Controller\uff1aRouting\/Controller.php"},"content":{"rendered":"<p>Laravel\u7684Controller\u4e3b\u8981\u63d0\u4f9bfilter\u7684\u529f\u80fd\u3002<br \/>\n\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u5728<a href=\"https:\/\/blog.turn.tw\/?page_id=866\" title=\"\u57f7\u884c\u5de5\u4f5c\uff1aRouting\/Route.php\">\u57f7\u884c\u5de5\u4f5c\uff1aRouting\/Route.php<\/a>\u88cf\u9762\u6211\u5011\u770b\u5230\uff0cRoute\u985e\u5225\u6709\u8b8a\u6578\u53bb\u5132\u5b58before\u8207after\u7b49filter\u52d5\u4f5c\u3002\u7136\u800c\u6b64\u8655\u7684Controller\u985e\u5225\u53c8\u6709beforeFilters\u8207afterFilters\u53bb\u5132\u5b58filter\u52d5\u4f5c\u3002<br \/>\n\u9019\u6a23\u7684\u8a2d\u8a08\u5141\u8a31\u4e86filter\u7684\u5169\u7a2e\u5beb\u6cd5\uff1a\u4e00\u7a2e\u662f\u5728\u8a2d\u5b9aroutes\u898f\u5247\u6642\u64b0\u5beb\u3001\u4e00\u7a2e\u662f\u5beb\u5728controller\u5167\u3002\u53c3\u898b\u5b98\u7db2\u300cController Filters\u300d\u4e00\u7bc0\u7684\u8209\u4f8b\u3002<\/p>\n<pre>\r\n<?php namespace Illuminate\\Routing;\r\n\r\nuse Closure;\r\nuse Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException;\r\n\r\nabstract class Controller {\r\n\r\n\t\/**\r\n\t * The \"before\" filters registered on the controller.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected $beforeFilters = array();\r\n\r\n\t\/**\r\n\t * The \"after\" filters registered on the controller.\r\n\t *\r\n\t * @var array\r\n\t *\/\r\n\tprotected $afterFilters = array();\r\n\r\n\t\/**\r\n\t * The route filterer implementation.\r\n\t *\r\n\t * @var \\Illuminate\\Routing\\RouteFiltererInterface\r\n\t *\/\r\n\tprotected static $filterer;\r\n\r\n\t\/**\r\n\t * The layout used by the controller.\r\n\t *\r\n\t * @var \\Illuminate\\View\\View\r\n\t *\/\r\n\tprotected $layout;\r\n\r\n\t\/**\r\n\t * Register a \"before\" filter on the controller.\r\n\t *\r\n\t * @param  \\Closure|string  $filter\r\n\t * @param  array  $options\r\n\t * @return void\r\n\t *\/\r\n\tpublic function beforeFilter($filter, array $options = array())\r\n\t{\r\n\t\t$this->beforeFilters[] = $this->parseFilter($filter, $options);\r\n\t}\r\n\r\n\t\/**\r\n\t * Register an \"after\" filter on the controller.\r\n\t *\r\n\t * @param  \\Closure|string  $filter\r\n\t * @param  array  $options\r\n\t * @return void\r\n\t *\/\r\n\tpublic function afterFilter($filter, array $options = array())\r\n\t{\r\n\t\t$this->afterFilters[] = $this->parseFilter($filter, $options);\r\n\t}\r\n\r\n\t\/**\r\n\t * Parse the given filter and options.\r\n\t *\r\n\t * @param  \\Closure|string  $filter\r\n\t * @param  array  $options\r\n\t * @return array\r\n\t *\/\r\n\tprotected function parseFilter($filter, array $options)\r\n\t{\r\n\t\t$parameters = array();\r\n\r\n\t\t$original = $filter;\r\n\r\n\t\tif ($filter instanceof Closure)\r\n\t\t{\r\n\t\t\t$filter = $this->registerClosureFilter($filter);\r\n\t\t}\r\n\t\telseif ($this->isInstanceFilter($filter))\r\n\t\t{\r\n\t\t\t$filter = $this->registerInstanceFilter($filter);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tlist($filter, $parameters) = Route::parseFilter($filter);\r\n\t\t}\r\n\r\n\t\treturn compact('original', 'filter', 'parameters', 'options');\r\n\t}\r\n\r\n\t\/**\r\n\t * Register an anonymous controller filter Closure.\r\n\t *\r\n\t * @param  \\Closure  $filter\r\n\t * @return string\r\n\t *\/\r\n\tprotected function registerClosureFilter(Closure $filter)\r\n\t{\r\n\t\t$this->getFilterer()->filter($name = spl_object_hash($filter), $filter);\r\n\r\n\t\treturn $name;\r\n\t}\r\n\r\n\t\/**\r\n\t * Register a controller instance method as a filter.\r\n\t *\r\n\t * @param  string  $filter\r\n\t * @return string\r\n\t *\/\r\n\tprotected function registerInstanceFilter($filter)\r\n\t{\r\n\t\t$this->getFilterer()->filter($filter, array($this, substr($filter, 1)));\r\n\r\n\t\treturn $filter;\r\n\t}\r\n\r\n\t\/**\r\n\t * Determine if a filter is a local method on the controller.\r\n\t *\r\n\t * @param  mixed  $filter\r\n\t * @return boolean\r\n\t *\r\n\t * @throws \\InvalidArgumentException\r\n\t *\/\r\n\tprotected function isInstanceFilter($filter)\r\n\t{\r\n\t\tif (is_string($filter) && starts_with($filter, '@'))\r\n\t\t{\r\n\t\t\tif (method_exists($this, substr($filter, 1))) return true;\r\n\r\n\t\t\tthrow new \\InvalidArgumentException(\"Filter method [$filter] does not exist.\");\r\n\t\t}\r\n\r\n\t\treturn false;\r\n\t}\r\n\r\n\t\/**\r\n\t * Remove the given before filter.\r\n\t *\r\n\t * @param  string  $filter\r\n\t * @return void\r\n\t *\/\r\n\tpublic function forgetBeforeFilter($filter)\r\n\t{\r\n\t\t$this->beforeFilters = $this->removeFilter($filter, $this->getBeforeFilters());\r\n\t}\r\n\r\n\t\/**\r\n\t * Remove the given after filter.\r\n\t *\r\n\t * @param  string  $filter\r\n\t * @return void\r\n\t *\/\r\n\tpublic function forgetAfterFilter($filter)\r\n\t{\r\n\t\t$this->afterFilters = $this->removeFilter($filter, $this->getAfterFilters());\r\n\t}\r\n\r\n\t\/**\r\n\t * Remove the given controller filter from the provided filter array.\r\n\t *\r\n\t * @param  string  $removing\r\n\t * @param  array  $current\r\n\t * @return array\r\n\t *\/\r\n\tprotected function removeFilter($removing, $current)\r\n\t{\r\n\t\treturn array_filter($current, function($filter) use ($removing)\r\n\t\t{\r\n\t\t\treturn $filter['original'] != $removing;\r\n\t\t});\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the registered \"before\" filters.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tpublic function getBeforeFilters()\r\n\t{\r\n\t\treturn $this->beforeFilters;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the registered \"after\" filters.\r\n\t *\r\n\t * @return array\r\n\t *\/\r\n\tpublic function getAfterFilters()\r\n\t{\r\n\t\treturn $this->afterFilters;\r\n\t}\r\n\r\n\t\/**\r\n\t * Get the route filterer implementation.\r\n\t *\r\n\t * @return \\Illuminate\\Routing\\RouteFiltererInterface\r\n\t *\/\r\n\tpublic static function getFilterer()\r\n\t{\r\n\t\treturn static::$filterer;\r\n\t}\r\n\r\n\t\/**\r\n\t * Set the route filterer implementation.\r\n\t *\r\n\t * @param  \\Illuminate\\Routing\\RouteFiltererInterface  $filterer\r\n\t * @return void\r\n\t *\/\r\n\tpublic static function setFilterer(RouteFiltererInterface $filterer)\r\n\t{\r\n\t\tstatic::$filterer = $filterer;\r\n\t}\r\n\r\n\t\/**\r\n\t * Create the layout used by the controller.\r\n\t *\r\n\t * @return void\r\n\t *\/\r\n\tprotected function setupLayout() {}\r\n\r\n\t\/**\r\n\t * Execute an action on the controller.\r\n\t *\r\n\t * @param  string  $method\r\n\t * @param  array   $parameters\r\n\t * @return \\Symfony\\Component\\HttpFoundation\\Response\r\n\t *\/\r\n\tpublic function callAction($method, $parameters)\r\n\t{\r\n\t\t$this->setupLayout();\r\n\r\n\t\t$response = call_user_func_array(array($this, $method), $parameters);\r\n\r\n\t\t\/\/ If no response is returned from the controller action and a layout is being\r\n\t\t\/\/ used we will assume we want to just return the layout view as any nested\r\n\t\t\/\/ views were probably bound on this view during this controller actions.\r\n\t\tif (is_null($response) && ! is_null($this->layout))\r\n\t\t{\r\n\t\t\t$response = $this->layout;\r\n\t\t}\r\n\r\n\t\treturn $response;\r\n\t}\r\n\r\n\t\/**\r\n\t * Handle calls to missing methods on the controller.\r\n\t *\r\n\t * @param  array   $parameters\r\n\t * @return mixed\r\n\t *\r\n\t * @throws \\Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException\r\n\t *\/\r\n\tpublic function missingMethod($parameters = array())\r\n\t{\r\n\t\tthrow new NotFoundHttpException(\"Controller method not found.\");\r\n\t}\r\n\r\n\t\/**\r\n\t * Handle calls to missing methods on the controller.\r\n\t *\r\n\t * @param  string  $method\r\n\t * @param  array   $parameters\r\n\t * @return mixed\r\n\t *\r\n\t * @throws \\BadMethodCallException\r\n\t *\/\r\n\tpublic function __call($method, $parameters)\r\n\t{\r\n\t\tthrow new \\BadMethodCallException(\"Method [$method] does not exist.\");\r\n\t}\r\n\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Laravel\u7684Controller\u4e3b\u8981\u63d0\u4f9bfilter\u7684\u529f\u80fd\u3002 \u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c\u5728\u57f7\u884c\u5de5\u4f5c\uff1aRouting\/R &hellip; <a href=\"https:\/\/blog.turn.tw\/?page_id=942\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">MVC Controller\uff1aRouting\/Controller.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\/942"}],"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=942"}],"version-history":[{"count":4,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/942\/revisions"}],"predecessor-version":[{"id":947,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/942\/revisions\/947"}],"wp:attachment":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}