[英]How to get route name from url (not current url) in Laravel?
為所有前端 url 處理動態路由,但在訪問admin
路由時,它進入中止條件,即在上述路由的 function 上。
Web.php
Route::get('/{slug?}', 'slug' )->where('slug','(.*)')->name('slug');
FrontController.php
public function slug(Request $request, $slug=null) {
if ($slug == "admin") {
return redirect()->route('login');
}
if (Str::contains($slug, 'admin/')) {
$routes = Route::getRoutes();
$request = Request::create($slug);
try {
$route->match($request,'admin.dashboard');
//How to access requested url's route name to redirect there
} catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e) {
abort(404);
}
}
if ($slug == "login") {
return view('auth.login');
}
if ($slug == null) {
$page = Pages::where('url', '')->first();
}
if (empty($page)) {
abort(404);
}
$contentWithBlade = Blade::render($page->pages_content);
$session = $request->session()->put('key', $page);
return view('frontend.pages.template', compact('contentWithBlade', 'page'));
}
有什么建議可以根據route url
獲取route
名稱嗎?
檢查這個
Route::getCurrentRoute()->getPath();
或者
\Request::route()->getName()
從 v5.1
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel v5.2
Route::currentRouteName(); //use Illuminate\Support\Facades\Route;
或者,如果您需要操作名稱
Route::getCurrentRoute()->getActionName();
檢索請求 URI
path 方法返回請求的 URI。 因此,如果傳入請求的目標是http://example.com/foo/bar
,則 path 方法將返回foo/bar
:
$uri = $request->path();
is
方法允許您驗證傳入請求 URI 是否與給定模式匹配。 使用此方法時,您可以使用*
字符作為通配符:
if ($request->is('admin/*')) {
//
}
要獲取完整的 URL,而不僅僅是路徑信息,您可以在請求實例上使用 url 方法:
$url = $request->url();
Laravel v5.3... v5.8
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
Laravel v6.x...7.x
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
** 當前截至 2019 年 11 月 11 日 - 版本 6.5 **
可以選擇使用請求獲取路線
$request->route()->getName();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.