个人手记 laravel 路由表
Route::get('login/',[ 'as' => 'Login', 'uses' => 'UserController@login' ]);
route("Login");显示不了 http:// xxx.com/login/ 统一显示为 http:// xxx.com/login
查看路由表的代码是trim这个方法,他把结尾的斜杠全部去掉了
总共三个文件修改四处地方,都是去掉trim
Illuminate\Routing\Route.php
新增 protected $olduri;
public function olduri()
{
return $this->olduri;
}
public function __construct($methods, $uri, $action , $olduri) { $this->uri = $uri; $this->olduri = $olduri; $this->methods = (array) $methods; $this->action = $this->parseAction($action); if (in_array('GET', $this->methods) && ! in_array('HEAD', $this->methods)) { $this->methods[] = 'HEAD'; } if (isset($this->action['prefix'])) { $this->prefix($this->action['prefix']); } }
Illuminate\Routing\Router.phpprotected function createRoute($methods, $uri, $action) { // If the route is routing to a controller we will parse the route action into // an acceptable array format before registering it and creating this route // instance itself. We need to build the Closure that will call this out. if ($this->routingToController($action)) { $action = $this->getControllerAction($action); } $route = $this->newRoute( $methods,$this->prefix($uri), $action , $uri ); // If we have groups that need to be merged, we will merge them now after this // route has already been created and is ready to go. After we're done with // the merge we will be ready to return the route back out to the caller. if ( ! empty($this->groupStack)) { $this->mergeController($route); } $this->addWhereClausesToRoute($route); return $route; }
新增$olduriprotected function newRoute($methods, $uri, $action , $olduri)
{
return new Route($methods, $uri, $action , $olduri);
}Illuminate\Routing\UrlGenerator.php
protected function replaceRouteParameters($path, array &$parameters) { if (count($parameters)) { $path = preg_replace_sub( '/\{.*?\}/', $parameters, $this->replaceNamedParameters($path, $parameters) ); } return trim(preg_replace('/\{.*?\?\}/', '', $path), '/'); }改为protected function replaceRouteParameters($path, array &$parameters) { if (count($parameters)) { $path = preg_replace_sub( '/\{.*?\}/', $parameters, $this->replaceNamedParameters($path, $parameters) ); } return preg_replace('/\{.*?\?\}/', '', $path); }protected function trimUrl($root, $path, $tail = '') { return trim($root.'/'.trim($path.'/'.$tail, '/'), '/'); }修改为protected function trimUrl($root, $path, $tail = '') {return $root."/".ltrim($path , '/').($tail?'/'.$tail:'');}$route->uri() 修改为 $route->olduri();
protected function toRoute($route, array $parameters, $absolute) { $domain = $this->getRouteDomain($route, $parameters); $uri = strtr(rawurlencode($this->trimUrl( $root = $this->replaceRoot($route, $domain, $parameters), $this->replaceRouteParameters($route->olduri(), $parameters) )), $this->dontEncode).$this->getRouteQueryString($parameters); return $absolute ? $uri : '/'.ltrim(str_replace($root, '', $uri), '/'); }欢迎吐槽