Reputation: 333
I want to know is there any possibility to reduce routes for same controller in laravel4.
Here is my route:
Route::get('emp/add-employee/','EmpController@addEmployee');
Route::post('emp/add-employee/','EmpController@addEmployee');
Route::get('emp/edit-employee/{id}','EmpController@editEmployee');
Route::post('emp/edit-employee/{id}','EmpController@editEmployee');
Route::get('emp/view-employee/{id}','EmpController@viewEmployee');
is there any posibility to do reduce...?
Upvotes: 2
Views: 105
Reputation: 44536
Your route actions look like the ones you'd find in a RESTful Resource Controller. So you could use this:
Route::resource('emp', 'EmpController', array('only' => array('create', 'store', 'edit', 'update', 'show')));
This will of course require you to rename the controller methods accordingly and the route paths will be a little different, but you'd have a more compact route definition and consistent naming. Below are the routes that are generated by the Route::resource
definition above.
+-----------------------------+---------------+-------------------------+
| GET emp/create | emp.create | EmpController@create |
| POST emp | emp.store | EmpController@store |
| GET emp/{id} | emp.show | EmpController@show |
| GET emp/{id}/edit | emp.edit | EmpController@edit |
| PUT emp/{id} | emp.update | EmpController@update |
+-----------------------------+---------------+-------------------------+
So you'd have to rename your controller method names like so:
GET : addEmployee() -> create() // shows the add form
POST: addEmployee() -> store() // processes the add form when submitted
GET : editEmployee() -> edit() // shows the edit form
POST: editEmployee() -> update() // processes the edit form when submitted
GET : viewEmployee() -> show()
Upvotes: 5
Reputation: 5943
You could use controller routes.
Route::controller('emp', 'EmpController');
Now you just have to rename the functions within your controller to also represent the method used like this:
public function getAddEmloyee()
public function postAddEmloyee()
public function getEditEmployee($id)
etc.
See also the Laravel docs for controllers
Upvotes: 2
Reputation: 168725
Yes, use Route::match()
. This will allow you to specify GET
and POST
in a single route call, like so:
Route::match(['GET', 'POST'], 'emp/edit-employee/{id}','EmpController@editEmployee');
You can also use Route::all()
which will match any type request, which includes GET
and POST
and also any other HTTP verbs that may be specified, if that's what you want.
Upvotes: 1