Laravel URL 处理:通过 URL 获取路由参数 0 个改进

问题描述

Wiki:Laravel 视图:循环变量 $loop

如何通过上面的 URL 来取路由名称和路由参数?

解决方法

$request = app('request')->create('https://learnku.com/laravel/wikis/26002');

$route = app('router')->getRoutes()->match($request);

$route->getName();
$route->getAction();
$route->parameters();

输出:

>>> $request = app('request')->create('https://learnku.com/laravel/wikis/26002');
=> Illuminate\Http\Request {#1813
  .
  .
  .
   }
>>>
>>> $route = app('router')->getRoutes()->match($request);
=> Illuminate\Routing\Route {#747
  .
  .
  .
   }
>>>
>>> $route->getName();
=> "wikis.show"
>>> $route->getAction();
=> [
     "middleware" => [
       "web",
       "restrict_web_access",
     ],
     "uses" => "App\Http\Controllers\WikisController@show",
     "controller" => "App\Http\Controllers\WikisController@show",
     "namespace" => "App\Http\Controllers",
     "prefix" => null,
     "where" => [],
     "as" => "wikis.show",
   ]
>>> $route->parameters();
=> [
     "community_slug" => "laravel",
     "wiki" => "26002",
   ]

参考

本文为 Wiki 文章,邀您参与纠错、纰漏和优化
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!