如何在Laravel中获取已注册路由路径的列表?
所有路由都存储在routes/文件夹中。如果打开routes/web.php,你将看到为你的应用程序定义的路由列表。如果你想使用路由,你需要包含以下类:
use Illuminate\Support\Facades\Route;
routes/web.php文件以及上面的类在安装Laravel时默认添加。
这是一个基本路由,当你访问URL **https://127.0.0.1:8000/** 时会被调用。它被称为默认路由。当访问URL时,会调用view('test')。
use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('test'); });
让我们了解如何获取所有已注册路由的列表。
示例1
(使用artisan命令)
获取应用程序中使用的所有路由的命令是: php artisan route:list
php artisan route:list
输出
以上代码的输出是:
C:\xampp\htdocs\laraveltest>php artisan route:list +--------+----------+---------------------+------+-------------------------------------------------------------+------------------------------------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+---------------------+------+-------------------------------------------------------------+------------------------------------------+ | | GET|HEAD | / | | Closure | web | | | GET|HEAD | api/user | | Closure | api | | | | | | | App\Http\Middleware\Authenticate:sanctum | | | GET|HEAD | sanctum/csrf-cookie | | Laravel\Sanctum\Http\Controllers\CsrfCookieController@show | web | | | GET|HEAD | test | | App\Http\Controllers\StudentController@index | web | | | GET|HEAD | testarray | | App\Http\Controllers\testuserip@index | web | | | GET|HEAD | users | | App\Http\Controllers\UserController@index | web | | | GET|HEAD | validation | | App\Http\Controllers\testvalidationController@showform | web | | | POST | validation | | App\Http\Controllers\testvalidationController@validateform | web | +--------+----------+---------------------+------+-------------------------------------------------------------+------------------------------------------+
使用**route:list**,还会显示每个路由可用的中间件。如果你想避免显示中间件路由,可以按照以下示例操作。
示例2
在route:list中避免中间件
跳过路由列表中中间件路由的命令如下: php artisan route:list --except=web
php artisan route:list -v
输出
上面的输出是:
C:\xampp\htdocs\laraveltest>php artisan route:list -v +--------+----------+---------------------+------+------------------------------------------------------------+------------------------------------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+---------------------+------+------------------------------------------------------------+------------------------------------------+ | | GET|HEAD | / | | Closure | web | | | GET|HEAD | api/user | | Closure | api | | | | | | | App\Http\Middleware\Authenticate:sanctum | | | GET|HEAD | routes | | Closure | web | | | GET|HEAD | sanctum/csrf-cookie | | Laravel\Sanctum\Http\Controllers\CsrfCookieController@show | web | | | GET|HEAD | test | | App\Http\Controllers\StudentController@index | web | | | GET|HEAD | testarray | | App\Http\Controllers\testuserip@index | web | | | GET|HEAD | users | | App\Http\Controllers\UserController@index | web | | | GET|HEAD | validation | | App\Http\Controllers\testvalidationController@showform | web | | | POST | validation | | App\Http\Controllers\testvalidationController@validateform | web | +--------+----------+---------------------+------+------------------------------------------------------------+------------------------------------------+ C:\xampp\htdocs\laraveltest>
示例3
使用getRoutes()方法
要使用**getRoutes()**方法,你需要包含Route类:
use Illuminate\Routing\Router;
使用**getRoutes()**,你将得到一个RouteCollection。需要循环遍历它来获取所有路由的列表。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; use Illuminate\Routing\Router; class testuserip extends Controller { public function index(Router $route) { $r = $route->getRoutes(); foreach ($r as $value) { echo $value->uri(); echo "<br/>"; } } }
输出
以上代码的输出如下:
_ignition/health-check _ignition/execute-solution _ignition/share-report _ignition/scripts/{script} _ignition/styles/{style} sanctum/csrf-cookie api/user / testarray test users validation validation
示例4
在web.php中使用Route类使用getRoutes()。**getRoutes()**将返回包含所有路由列表的routeCollection。
use Illuminate\Support\Facades\Route; Route::get('/routes', function() { $routeCollection = Route::getRoutes(); foreach ($routeCollection as $value) { echo $value->getActionName(); echo "<br/>"; } });
输出
以上代码的输出是:
Facade\Ignition\Http\Controllers\HealthCheckController Facade\Ignition\Http\Controllers\ExecuteSolutionController Facade\Ignition\Http\Controllers\ShareReportController Facade\Ignition\Http\Controllers\ScriptController Facade\Ignition\Http\Controllers\StyleController Laravel\Sanctum\Http\Controllers\CsrfCookieController@show Closure Closure App\Http\Controllers\testuserip@index App\Http\Controllers\StudentController@index App\Http\Controllers\UserController@index App\Http\Controllers\testvalidationController@showform App\Http\Controllers\testvalidationController@validateform Closure
广告