programing

라라벨:현재 루트명을 취득하는 방법(v5...v7)

bestcode 2022. 9. 17. 10:46
반응형

라라벨:현재 루트명을 취득하는 방법(v5...v7)

Laravel v4에서는 현재 노선명을 취득할 수 있었습니다.

Route::currentRouteName()

Laravel v5Laravel v6에서는 어떻게 해야 하나요?

이거 드셔보세요

Route::getCurrentRoute()->getPath();

또는

\Request::route()->getName()

v5.1부터

use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();

라라벨 v5.2

Route::currentRouteName(); //use Illuminate\Support\Facades\Route;

또는 작업 이름이 필요한 경우

Route::getCurrentRoute()->getActionName();

Larabel 5.2 루트 매뉴얼

Request URI

URI를 , 가 「URI」를 대상으로 있는 는, 「URI」를 대상으로 하고 있습니다. 따라서, 수신 요청이 다음 대상으로 지정되면http://example.com/foo/bar는 를 foo/bar:

$uri = $request->path();

is 를 하면, 가 pattern.method ", "URI" 와 할 수 있습니다..*할 때 합니다.

if ($request->is('admin/*')) {
    //
}

경로 정보뿐만 아니라 완전한 URL을 얻으려면 요청 인스턴스에서 url 메서드를 사용합니다.

$url = $request->url();

Larabel v5.3 ...v5.8

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

Larabel 5.3 루트 매뉴얼

Laravel v6.x...7.x

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

** 2019년 11월 11일 현재 - 버전 6.5 **

Larabel 6.x 루트 매뉴얼

요청을 사용하여 경로를 가져오는 옵션이 있습니다.

$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();

여러 경로에서 메뉴를 선택할 경우 다음과 같이 할 수 있습니다.

<li class="{{ (Request::is('products/*') || Request::is('products') || Request::is('product/*') ? 'active' : '') }}"><a href="{{url('products')}}"><i class="fa fa-code-fork"></i>&nbsp; Products</a></li>

또는 단일 메뉴만 선택하려면 다음과 같이 하십시오.

<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i>&nbsp; Users</a></li>

Larabel 5.2에서도 테스트 완료

이게 도움이 됐으면 좋겠네요.

루트명이 아닌 URL이 필요한 경우 다른 클래스를 사용하거나 필요하지 않습니다.

url()->current();

Larabel 5.2 사용 가능

$request->route()->getName()

현재 경로 이름이 표시됩니다.

5.2 에서는, 다음과 같이 요구를 직접 사용할 수 있습니다.

$request->route()->getName();

또는 도우미 방법을 통해:

request()->route()->getName();

출력 예:

"home.index"

현재 경로 접근

블레이드 템플릿에서 현재 경로 이름 가져오기

{{ Route::currentRouteName() }}

자세한 것은, https://laravel.com/docs/5.5/routing#accessing-the-current-route 를 참조해 주세요.

라라벨 7 또는 8에서는 도우미 기능을 사용합니다.

Get Current Route Name
request()->route()->getName()

매크로를 사용하여 요청 클래스에 대한 고유한 메서드를 만들기 위해 루트가 현재보다 나은지 여부를 확인하려면

»AppServiceProvider »boot 삭제:

use Illuminate\Support\Facades\Request;
public function boot()
    {
        Request::macro('isCurrentRoute', function ($routeNames) {
            $bool = false;
            foreach (is_array($routeNames) ? $routeNames : explode(",",$routeNames) as $name) {
               if(request()->routeIs($name)) {
                   $bool = true;
                   break;
                }
             }

             return $bool;
        });
    }

블레이드 또는 컨트롤러에서 이 방법을 사용할 수 있습니다.

request()->isCurrentRoute('foo') // string route
request()->isCurrentRoute(['bar','foo','xyz.*']) //array routes
request()->isCurrentRoute('blogs,foo,bar,xyz.*') //string route seperated by comma

내장된 라라벨 루트 방식을 사용할 수 있습니다.

request()->routeIs('home');
request()->routeIs('blogs.*'); //using wildcard

빠른 파사드입니다.\Route::current()->getName()

이것은 5.4라벨에서도 동작합니다.*

컨트롤러 액션에서는 다음 작업을 수행할 수 있습니다.

public function someAction(Request $request)
{
    $routeName = $request->route()->getName();
}

$request이 문제는 라라벨의 서비스 컨테이너에 의해 해결됩니다.

getName()는 이름 있는 루트의 루트 이름만 반환합니다.null 않은 경우, ""를 할 수 .\Illuminate\Routing\Route다른 관심사에 대해 이의를 제기합니다.)

즉, "name Of My Route"를 반환하려면 다음과 같이 루트를 정의해야 합니다.

Route::get('my/some-action', [
    'as' => 'nameOfMyRoute',
    'uses' => 'MyController@someAction'
]);

템플릿에서 사용할 수 있습니다.

<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>

Bellow 코드를 사용하여 블레이드 파일의 경로 이름을 가져올 수 있습니다.

request()->route()->uri

★★★★★★★★★★★★★★★★★★★★★★★★★★」5.3시도했던 만들 수 것 같습니다.

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

https://laravel.com/docs/5.3/routing#accessing-the-current-route

Current Route(v5.3 이후)로의 액세스

루트 파사드에서 currentRouteName 및 currentRouteAction 메서드를 사용하여 착신 요구를 처리하는 루트에 대한 정보에 액세스할 수 있습니다.

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

접근 가능한 모든 메서드를 확인하려면 루트 파사드와 루트인스턴스 양쪽의 기본 클래스에 대한 API 매뉴얼을 참조해 주세요.

참고 자료 : https://laravel.com/docs/5.2/routing#accessing-the-current-route

제 생각에 가장 쉬운 해결책은 이 도우미를 사용하는 것입니다.

request()->route()->getName()

문서에 대해서는 다음 링크를 참조하십시오.

Request::path(); 경우, 「 」를 말아 주세요.use Request;

$request->route()->getName();

라라벨 도우미와 마법의 방법을 사용하다

request()->route()->getName()

있습니다.\Illuminate\Routing\Router.php 하다, 하다, 하다, 하다, 하다, 하다, 이렇게 할 수 있어요.currentRouteNamed()컨트롤러 방식으로 라우터를 주입합니다.예를 들어 다음과 같습니다.

use Illuminate\Routing\Router;
public function index(Request $request, Router $router) {
   return view($router->currentRouteNamed('foo') ? 'view1' : 'view2');
}

또는 루트 파사드를 사용합니다.

public function index(Request $request) {
   return view(\Route::currentRouteNamed('foo') ? 'view1' : 'view2');
}

도 이런 할 수 .is()가 지정된 중 여부를 하려면 이 을 사용합니다.preg_match()등)으로 한 동작을 'foo.bar.done'preg_match()PHP를 사용하다

public function index(Request $request) {
    return view(\Route::is('foo', 'bar') ? 'view1' : 'view2');
}

클래스 맨 위에 네임스페이스를 Import합니다.

use Illuminate\Support\Facades\Route;

라라벨 v8

$route = Route::current(); // Illuminate\Routing\Route
$name = Route::currentRouteName(); // RouteName
$action = Route::currentRouteAction(); // Action

Larabel v7, 6 및 5.8

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

5.3의 루트명을 취득하기 위해서 사용해 왔습니다.

Request::path()

해결책:

$routeArray = app('request')->route()->getAction();
$controllerAction = class_basename($routeArray['controller']);
list($controller, $route) = explode('@', $controllerAction);
echo $route;

다음 방법을 사용할 수 있습니다.

Route::getCurrentRoute()->getPath();

Larabel version > 6.0 에서는, 다음의 방법을 사용할 수 있습니다.

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

컨트롤러에서의 현재 루트 이름 접근

ie - http://localhost/your_project_name/edit

$request->segment(1);  // edit

(또는 )

$request->url();  // http://localhost/your_project_name/edit

도우미 파일에서

당신의사용할 수 있습니다 사용할 수 있다.Route::current()->uri()현재 URL.현재 URL을 가져옵니다 가기 위해서입니다.

따라서 루트명을 비교하여 메뉴에서 액티브클래스를 설정하는 경우는,

Route::currentRouteName()그리고이름을따고 비교하다 compare노선로의 이름을 가져오려면.

어떤 이유로 나는 어떤 이 솔루션을 사용할 수 없었습니다어떤 이유로든, 저는 이 솔루션들 중 어떤 것도 쓸 수 없었습니다. 그래서 나는그래서 You에서 내 노선을 선언했다길을 선언했어 내 그냥.web.php~하듯이로$router->get('/api/v1/users', ['as' => 'index', 'uses' => 'UserController@index'])그리고 나의 컨트롤러에 내가 길 내 컨트롤러에서를 사용하는 것의 이름이름을 얻었는데 루트읬다.$routeName = $request->route()[1]['as'];는 어떤.$request있이\Illuminate\Http\Request $request입력 매개 변수에typehinted 매개 변수indexUserController

Lumen 5.6을 사용합니다.도움이 됐으면 좋겠는데

당신은:코드 이 쓰여질 수 있다.url()->current()

블레이드 파일에서는:{{url()->current()}}

그것을 하는 방법은 여러 가지가 있다.다음을 입력할 수 있습니다.

\Illuminate\Support\Facades\Request::route()->getName()

루트명을 취득합니다.

뷰상의 루트명 또는 URL ID가 뷰상의 루트명에 대해 직접 필요한 경우, 아무도 응답할 수 없습니다.

$routeName = Request::route()->getName();

뷰의 URL에서 ID를 확인합니다.

$url_id = Request::segment(2);

언급URL : https://stackoverflow.com/questions/30046691/laravel-how-to-get-current-route-name-v5-v7

반응형