Can we have a single controller for multiple routes, and get the parameter?
Currently, I have these routes:
Route::resource('/customers', 'CustomerController');
Route::resource('/agents', 'AgentController');
And a CustomerController
and a AgentController
with all resource functions working.
But as CustomerController
and AgentController
are almost same except for one database field, i.e. group_id
. I was thinking to use one controller i.e. PartyController
and one route as:
Route::resource('/parties/customers', 'PartyController ');
Route::resource('/parties/agents', 'PartyController ');
or if someone suggests:
Route::resource('/parties/{group}', 'PartyController ');
I have been searching for a while but finding it hard to follow this path. I've added this code in the constructor of PartyController
, to check the calling route:
$path = Request::capture()->path();
$this->group = ucwords(explode("/", $path)[1]);
echo($this->group );
All seems to be going well till here. But when in my index.blade.php
, I have this statement:
<p>{{ link_to_route('parties.create', 'Add new') }}</p>
I get an exception:
Route [parties.create] not defined.
I've tried multiple combinations, without any success and more errors come through, like accessing /parties/customers/create
doesn't work now.
So, is it possible anyway or should I abandon this idea?
EDIT: My question is different from Same Laravel resource controller for multiple routes as I am not using a trait.