Call to undefined method Route::name()

百般思念 提交于 2019-12-11 05:42:44

问题


im trying to set a name for my route

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

accourding to documentation

Instead of specifying the route name in the route array definition, you may chain the name method onto the end of the route definition:

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

so i wrote

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

but i get

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

回答1:


This import is wrong:

use Illuminate\Routing\Route;

You actually don't have to import any class as Laravel registers a global alias Route.

If you want to import the right class, that would be:

use Illuminate\Support\Facades\Route;

on top of route.php file

EDIT: this problem Works fine for me... Are you sure you have at least Laravel 5.1 version? This feature wasn't available in 5.0

If not : You need to run

composer update

in your terminal to get the latest Laravel 5.1.* version.




回答2:


For me ->name() only works like this:

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



回答3:


Try this route :

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

What version laravel you used? I think it's version affect to the function.



来源:https://stackoverflow.com/questions/38111281/call-to-undefined-method-routename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!