Layouts depending on module?

Hello!

I just could not figure out how to in the best way approach of having a certain layout depending on accessed module? I'm using the simple approach to modules,

just a subdir in the controller folder since i need share most of the models between my two (soon three) modules.

Any ideas on how to come around this problem?

Thanks!

/Martin

Try this in your base controller's init() method:



if($this->module)


    $this->layout=...related with $this->module->id...


Thanks!

That should do it. Adding this to the layout section, howto modify layout within a controller would be great to new users just like myself. I will try to compile my problems and solutions building my app and post them when im finished.

/Martin

Thanks a lot! There's been so many days I want to ask this. But I though that was no way. Glad to see I was wrong.

EDIT:

adding the code posted by Qiang, I got this error:

"Missing argument 1 for CModule::getModule()"

This is caused by the line

if($this->module)

Some doubts:

  • Why do I have to use the condition above?

  • Is it better to create my layouts under modules or in the main project? In mean, under myproject/protected/modules/mymodule/views/layout or myproject/protected/views/layout?

  • Under my module, how to create URLs that will ever be prefixed by the module-id? I mean, if I have a module 'docs', my urls will ever be 'http://myproject/docs/…'? (I've already set urlFormat to 'path')

Thx!

Quote

- Under my module, how to create URLs that will ever be prefixed by the module-id? I mean, if I have a module 'docs', my urls will ever be 'http://myproject/docs/...'? (I've already set urlFormat to 'path')

Make sure you configure your server to rewrite the url aswell.

You should be able to use the same rewrites as they use in the Zend framework.

URL: http://framework.zen…ur+URL+Rewriter

urlManager => 'path' just like you said you have it configured.

I've honestly not looked through the docs for this info so it might already be there, otherwise it might be good to add the link or copy paste the different configs.

/Martin

The code I suggested is meant to be placed inside the init() method of base controller class. Not all controllers belong to a module, and that’s why the check.

Quote

Make sure you configure your server to rewrite the url aswell.

You should be able to use the same rewrites as they use in the Zend framework.

URL: http://framework.zen…ur+URL+Rewriter

urlManager => 'path' just like you said you have it configured.

I've honestly not looked through the docs for this info so it might already be there, otherwise it might be good to add the link or copy paste the different configs.

/Martin

Not exactly i was looking for :confused:

There is a way to create a url (with createUrl) with something like Yii:app->getModule('mymodule')->createUrl('/post/admin')?

Quote

The code I suggested is meant to be placed inside the init() method of base controller class. Not all controllers belong to a module, and that's why the check.

Thx Qiang, I've misunderstood. But it wouldn't be better to put it in the module base class?

Yes, putting in the module is fine too, but then you just need to access $this instead of $this->module since the former already gives you the module instance.