현재 경로 이름을 얻는 방법?
Laravel 4에서는 다음을 사용하여 현재 경로 이름을 얻을 수있었습니다 ...
Route::currentRouteName()
Laravel 5에서 어떻게 할 수 있습니까?
이 시도
Route::getCurrentRoute()->getPath();
또는
\Request::route()->getName()
v5. + 이상
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
라 라벨 5.3
Route::currentRouteName(); //use Illuminate\Support\Facades\Route;
또는 액션 이름이 필요한 경우
Route::getCurrentRoute()->getActionName();
Laravel API에서 laravel 경로에 대한 모든 것을 찾을 수 있습니다 : http://laravel.com/api/5.0/Illuminate/Routing/Router.html http://laravel.com/api/5.0/Illuminate/Routing.html
요청 URI 검색
경로 메소드는 요청의 URI를 리턴합니다. 따라서 수신 요청이 대상으로 http://example.com/foo/bar
지정된 경우 경로 메소드는 다음을 리턴합니다 foo/bar
.
$uri = $request->path();
이 is
메소드를 사용하면 수신 요청 URI가 주어진 패턴과 일치하는지 확인할 수 있습니다. *
이 방법을 사용할 때 문자를 와일드 카드로 사용할 수 있습니다 .
if ($request->is('admin/*')) {
//
}
경로 정보뿐만 아니라 전체 URL을 얻으려면 요청 인스턴스에서 url 메소드를 사용할 수 있습니다.
$url = $request->url();
라 라벨 5.2 및 5.3
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
경로를 얻기 위해 요청을 사용하는 옵션이 있습니다
$request->route()->getName();
Laravel 5.1을 사용하면
\Request::route()->getName()
laravel v5 , v5.1.28 및 v5.2.10에 대한 현재 경로 이름을 찾는 방법을 찾았 습니다.
네임 스페이스
use Illuminate\Support\Facades\Route;
과
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel laravel v5.3의 경우 다음을 사용할 수 있습니다.
use Illuminate\Support\Facades\Route;
Route::currentRouteName();
당신이 필요로하는 경우 url로 하지 경로 이름을 , 당신은 어떤 다른 클래스를 필요로 / 사용할 필요가 없습니다 :
url()->current();
여러 경로에서 메뉴를 선택하려면 다음과 같이하십시오.
<li class="{{ (Request::is('products/*') || Request::is('products') || Request::is('product/*') ? 'active' : '') }}"><a href="{{url('products')}}"><i class="fa fa-code-fork"></i> Products</a></li>
또는 단일 메뉴 만 선택하려면 다음과 같이하면됩니다.
<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i> Users</a></li>
Laravel 5.2 에서도 테스트
이것이 누군가를 돕기를 바랍니다.
Laravel 5.2 사용할 수 있습니다
$request->route()->getName()
현재 경로 이름을 알려줍니다.
5.2에서는 다음과 같이 직접 요청을 사용할 수 있습니다.
$request->route()->getName();
or via the helper method:
request()->route()->getName();
Output example:
"home.index"
Shortest way is Route facade \Route::current()->getName()
This also works in laravel 5.4.*
You can use in template:
<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>
Now in Laravel 5.3
I am seeing that can be made similarly you tried:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
https://laravel.com/docs/5.3/routing#accessing-the-current-route
In a controller action, you could just do:
public function someAction(Request $request)
{
$routeName = $request->route()->getName();
}
$request
here is resolved by Laravel's service container.
getName()
returns the route name for named routes only, null
otherwise (but you could still explore the \Illuminate\Routing\Route
object for something else of interest).
In other words, you should have your route defined like this to have "nameOfMyRoute" returned:
Route::get('my/some-action', [
'as' => 'nameOfMyRoute',
'uses' => 'MyController@someAction'
]);
Request::path();
is better, and remember to Use Request;
Accessing The Current Route(v5.3 onwards)
You may use the current, currentRouteName, and currentRouteAction methods on the Route facade to access information about the route handling the incoming request:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
Refer to the API documentation for both the underlying class of the Route facade and Route instance to review all accessible methods.
Reference : https://laravel.com/docs/5.2/routing#accessing-the-current-route
I have used for getting route name in larvel 5.3
Request::path()
Looking at \Illuminate\Routing\Router.php
you can use the method currentRouteNamed()
by injecting a Router in your controller method. For example:
use Illuminate\Routing\Router;
public function index(Request $request, Router $router) {
return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
}
or using the Route facade:
public function index(Request $request) {
return view(\Route::currentRouteNamed('foo') ? 'view1' : 'view2');
}
You could also use the method is()
to check if the route is named any of the given parameters, but beware this method uses preg_match()
and I've experienced it to cause strange behaviour with dotted route names (like 'foo.bar.done'
). There is also the matter of performance around preg_match()
which is a big subject in the PHP community.
public function index(Request $request) {
return view(\Route::is('foo', 'bar') ? 'view1' : 'view2');
}
Accessing The Current Route
Get current route name in Blade templates
{{ Route::currentRouteName() }}
for more info https://laravel.com/docs/5.5/routing#accessing-the-current-route
$request->route()->getName();
In a Helper file,
Your can use Route::current()->uri()
to get current URL.
Hence, If you compare your route name to set active class on menu then it would be good if you use
Route::currentRouteName()
to get the name of route and compare
for some reasons, I couldn't use any of these solutions. so I just declared my route in web.php
as $router->get('/api/v1/users', ['as' => 'index', 'uses' => 'UserController@index'])
and in my controller I got the name of the route using $routeName = $request->route()[1]['as'];
which $request
is \Illuminate\Http\Request $request
typehinted parameter in index
method of UserController
using Lumen 5.6. Hope it would help someone.
Solution :
$routeArray = app('request')->route()->getAction();
$controllerAction = class_basename($routeArray['controller']);
list($controller, $route) = explode('@', $controllerAction);
echo $route;
In my opinion the most easiest solution is using this helper:
request()->route()->getName()
For the docs, see this link
참고URL : https://stackoverflow.com/questions/30046691/how-to-get-current-route-name
'IT' 카테고리의 다른 글
각도 2 @ViewChild 주석은 정의되지 않은 값을 반환합니다. (0) | 2020.05.12 |
---|---|
PHP에서 JavaScript 함수를 호출하는 방법? (0) | 2020.05.12 |
Visual Studio 2010은 항상 프로젝트가 오래되었다고 생각하지만 아무것도 변경되지 않았습니다. (0) | 2020.05.12 |
C #에서 추상 클래스의 생성자 (0) | 2020.05.12 |
JavaScript 오류 (Uncaught SyntaxError : 예기치 않은 입력 끝) (0) | 2020.05.12 |