5如何获取路线动作名称

5如何获取路线动作名称

本文介绍了Laravel 5如何获取路线动作名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取当前的路线操作,但不确定如何执行此操作.在Laravel 4中,我使用的是Route::currentRouteAction(),但现在有所不同.

I'm trying to get the current route action, but I'm not sure how to go about it. In Laravel 4 I was using Route::currentRouteAction() but now it's a bit different.

我正在尝试在控制器中执行Route::getActionName(),但是它一直在给我找不到方法.

I'm trying to do Route::getActionName() in my controller but it keeps giving me method not found.

<?php namespace App\Http\Controllers;

use Route;

class HomeController extends Controller
{
    public function getIndex()
    {
        echo 'getIndex';
        echo Route::getActionName();
    }
}

推荐答案

在Laravel 5中,您应该使用Method或Constructor注入.这将满足您的要求:

In Laravel 5 you should be using Method or Constructor injection. This will do what you want:

<?php namespace App\Http\Controllers;

use Illuminate\Routing\Route;

class HomeController extends Controller
{
    public function getIndex(Route $route)
    {
        echo 'getIndex';
        echo $route->getActionName();
    }
}

这篇关于Laravel 5如何获取路线动作名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:22