I wonder if anybody's encountered a situation like this and if there's a way to get around it.
In my web routes I have a staff resource ...
Route::resource('staff', 'StaffController');
And in my api routes I have the same thing.
The reason being that the web resource index method serves up a blade template the the api resource index will send a list of staff members. Then I can handle pagination, sorting and searching on the client.
The problem comes when, in my navigation, I use route('staff.index'). It will return the api route.
Well, first of all - you should prefix your api routes with a api. to separate them. The reason for this is to avoid the confusion and issues with naming/acessing wrong routes.
@topvillas No. You got something confused there :)
'prefix' => 'api' !== 'as' => 'api.'
The prefix is for browser URL.
The as is for the names of the routes.
Example:
protected function mapApiRoutes()
{
Route::group([
'namespace' => $this->namespace,
'prefix' => 'api', // This will add prefix to the route URL
], function ($router) {
require base_path('routes/api.php');
});
Route::group([
'namespace' => $this->namespace,
'prefix' => 'api', // This will add prefix to the route URL
'as' => 'api.', // This will add prefix to the route names. Ex.: api.users.create
], function ($router) {
require base_path('routes/api.php');
});
}
No no. I'm talking about resource based routes. Their names are being doubled up because Laravel has no way of knowing whether the resource group is sitting in the api or the web routes files.
But anyway, it doesn't matter. I'm just using the plain url. But for obvious reasons I'd prefer it I could use named routes.