调用未定义的方法Route :: name()

时间:2016-06-29 22:49:28

标签: php laravel laravel-5.2

我试图为我的路线设置名称

Route::get('test/show/{id}' , 'TestController@show');

参与文件

  

而不是在路由数组定义中指定路由名称,   您可以将name方法链接到路径定义的末尾:

Route::get('user/profile','UserController@showProfile')->name('profile');

所以我写了

Route::get('test/show/{id}' , 'TestController@show')->name('testShow');

但是我得到了

FatalErrorException in routes.php line 28:
Call to undefined method Illuminate\Routing\Route::name()
in routes.php line 28
at FatalErrorException->__construct() in compiled.php line 1743
at HandleExceptions->fatalExceptionFromError() in compiled.php line 1738
at HandleExceptions->handleShutdown() in compiled.php line 0
in compiled.php line 17158
at RouteServiceProvider->App\Providers\{closure}() in compiled.php line 6819
at call_user_func() in compiled.php line 6819
at Router->group() in compiled.php line 17159
at RouteServiceProvider->map() in compiled.php line 925
at call_user_func_array() in compiled.php line 925
at Container->call() in compiled.php line 4858
at RouteServiceProvider->loadRoutes() in compiled.php line 4840
at RouteServiceProvider->boot() in compiled.php line 17153
at RouteServiceProvider->boot() in compiled.php line 925
at call_user_func_array() in compiled.php line 925
at Container->call() in compiled.php line 1518
at Application->bootProvider() in compiled.php line 1510
at Application->Illuminate\Foundation\{closure}() in compiled.php line 1511
at array_walk() in compiled.php line 1511
at Application->boot() in compiled.php line 1785
at BootProviders->bootstrap() in compiled.php line 1311
at Application->bootstrapWith() in compiled.php line 1928
at Kernel->bootstrap() in compiled.php line 1890
at Kernel->sendRequestThroughRouter() in compiled.php line 1880
at Kernel->handle() in index.php line 53
in index.php line 21
at {main}() in index.php line 0

3 个答案:

答案 0 :(得分:7)

导入错误:

  

使用Illuminate \ Routing \ Route;

当Laravel注册全局别名Route时,您实际上不必导入任何类。

如果要导入正确的类,那将是:

  

使用Illuminate \ Support \ Facades \ Route;

在route.php文件

之上

编辑: 这个问题 对我来说工作正常...你确定你至少有Laravel 5.1版本吗?此功能在5.0

中不可用

如果没有: 你需要运行

  

composer update

在您的终端中  获得最新的Laravel 5.1。*版本。

答案 1 :(得分:0)

对我来说->name()只能这样:

Route::get('test/show/{id}' , ['uses' => 'TestController@show'])->name('testShow');

答案 2 :(得分:0)

尝试这条路线:

Route::get( 'test/show/{id}', [ 'as' => 'testShow', 'uses' => 'TestController@show' ] );

您使用的是什么版本的laravel?我认为它的版本会对函数产生影响。