1

I want to have nested resources like that:

Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function () 
{
   Route::resource('/articles', 'ArticleController');

   Route::group(['prefix' => '/articles', 'as' => 'articles.'], function () {
        Route::resource('/types', 'ArticleTypeController');
    });
});

But nested route for "article/type" doesn't work, I check my ArticleTypeController outside the "article" route and work.

I really confused, Everybody can help me?

and here is my controller:

class ArticleTypeController extends Controller
{

    public function index()
    {
        $types = ArticleType::all();
        return view('manage.articles.types.index')->withtypes($types);
    }
}
3
  • /article/type or /articles/types? Check how you're defining and make sure you're using the correct value (plural vs non-plural) Commented Jun 18, 2018 at 15:49
  • no Tim it's plural Commented Jun 18, 2018 at 15:52
  • But nested route for "article/type" doesn't work What's wrong with that statement? Should that statement read article/type or articles/types? Commented Jun 18, 2018 at 15:53

1 Answer 1

1
Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function () 
{
   Route::get('articles/types', 'ArticleTypeController@articleTypeMethod');

   Route::resource('articles', 'ArticleController');    
   Route::resource('articles.types', 'ArticleTypeController');
});

for nested resources use articles.types. plural naming is good. now that manage/articles and manage/articles/1/types will work.

If you want to put a custom route, put it above the resource route if the controller has been used as a resource. see the articles/types [GET] route which maps to ArticleTypeController's articleTypeMethod. now this way http://localhost.com/manage/articles/types should work

here is the 5.1 documentation and it has been removed from documentation of 5.5. but have a look at what Taylor said about it here

it's not recommended on REST to use index function for articles/types, a nested resources index method is used like articles/{id}/types. for articles/types you need to create a new method.

but if you still want to do it like that. just make it like this

Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function () 
{
   Route::get('articles/types', 'ArticleTypeController@index');

   Route::resource('articles', 'ArticleController');    
   Route::resource('articles.types', 'ArticleTypeController', ['except' => ['index']]);
});
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks for your reply, But specifically I want to use manage/articles/types, that's my issue
is that what you want?
Isn't it possible that using group or resource anyway?
as it is a custom route it can't be used as a REST resource route. you need to create a specific method and call with http method to that route as I have mentioned. resource routes are just index,create,store,edit,update,destroy. check the documentation link I have provided :)
I got it. Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.