Controller and Subdirectory Conflict

Recently found interesting case on StackOverflow.com. Please follow this link..

I’ve created new application and tried to reproduce the same situation and it didn’t work for me too.

Controllers structure (copied directly from SO):


--BookmarksController.php

--bookmarks

----CategoriesController.php

When I tried to access bookmarks/categories, an exception has been thrown (unable to find an action called categories in BookmarksController). I haven’t found a word about such problems in docs.

How do you usually solve such problems?

I’m not sure it’s possible because Yii searches for controllers in the CWebApplication.controllerPath. Look at this method: http://www.yiiframework.com/doc/api/1.1/CWebApplication#createController-detail


$classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';

Having subdirectories in the controllers directory may be ambiguous, because when accessing "user/profile/update" it is hard to say, is "user" a module or a directory under controller path.

Also I think controllerMap is not what you need. So the only possible option I see is to create a module "bookmarks".

Thanks for your answer.

So, how do you usually resolve such issues with Yii?

Previously I was creating modules for each site section. But in the last project I put all controllers (20 or more) into the one directory, and I don’t have any problems with it, since all controllers have unique names (same as models names but with “Controller” in the end).

If you want a url "bookmarks/categories" for controller CategoryController, just add a url rule in the application config:




'bookmarks/categories'=>'category',



If the concern is about separating code into smaller chunks, you could write actions by extending CAction instead of writing actionXyz methods within the controller. These could be easily structured in subdirectories.




BookmarksController.php

bookmarks

-- CreateBookmarkAction.php

-- ListBookmarksAction.php

-- ...

-- categories

---- CreateBookmarkCategoryAction.php

---- ListBookmarkCategoriesAction.php

---- ...



BookmarkController then whould mainly delegate:




class BookmarkController extends Controller

{

  public function actions()

  {

    return array(

      'create'         => 'path.to.CreateBookmarkAction',

      'list'           => 'path.to.ListBookmarksAction',

      'createCategory' => 'path.to.CreateBookmarkCategoryAction',

      'listCategories' => 'path.to.ListBookmarkCategoriesAction',

      [...]

    );

  }

}



a nice solution :lol:

I like the way it looks!