Reputation:
i want to use custom function in Route::resource()
controller like with public function check()
, public function login()
or public function laogout()
, but my code doesnt work, how to use custom functions?
For Example:
Route:
Route::resource('auth', 'AuthenticationController');
View:
Controller:
public function check()
{
//
}
public function login()
{
//
}
public function logout()
{
//
}
I get this error:
NotFoundHttpException in RouteCollection.php line 161:
Upvotes: 3
Views: 2925
Reputation: 59
Of course it is possible
You can add as many methods as you want in each controller, Larval by default adds a curd to the resource controller, methods and routers, and you need to do the following to create your own modular method in a controller.
1) First add your method publicly in the corresponding controller file
public function echoUser($id)
{
return $id;
}
2) In the web file of the routers folder, write and name the router, for example:
Route :: get ('admin / echoUser / {id}', 'UserController @ echoUser') -> name ('admin.echoUser');
Note:After adding this router, you can see it in the list of registered larval routers with the following command
php artisan router: list
3) Now you can use in the blade template file to access this method and pass id to method easily
<a class="btn btn-warning btn-sm" href="{{route('admin.echoUser', $user-> id)}} "> Show </a>
OR
<form method="get" action="{{route('admin.echoUser' , $user->id)}}">
@csrf
<button type="submit" class="btn btn-primary btn-sm">show</button>
</form>
Upvotes: 1
Reputation: 40909
Firstly, resource controllers should be used to generate Restful API that provides CRUD interface to your application - not for logging users in and out.
Secondly, you can't add additional controller methods with Route::resource() - this method is used to define the prefixed set of methods - see http://laravel.com/docs/5.1/controllers#restful-resource-controllers for more information.
If you want to add those custom actions to your routing, you'll need to define them separately before your resource routes, e.g.:
Route::get('auth/check', 'AuthenticationController@check');
You can read more about defining custom routes here: http://laravel.com/docs/5.1/routing
Upvotes: 1
Reputation: 5443
If you use custom function in route resource,you need to define your request type with function name,like this
Route::POST('auth/save-comment','authController@customFunctionName');
Upvotes: 0