Can a module have its own confg file?

Can I have a seperate config file for my ‘admin’ module? If so, how do I reference to it?

Will it then be possible to have SEF URLs on the frontend but normal URLs on the backend?

Hi GSTAR!

In the documentation there are no reference to config file for modules.

In general I think that is quite hard to have the possibility to change url style in module, because when the UrlManager is parsing a url still doesn’t know wich module have to be loaded (and so how to parse it).

For other configuration parameters you can change the configuration in the file "SomethingModule extends CWebModule", but for url I think that there is a conceptual problem.

I advise to create a separate application “backend”. So you’ll be able to configure it as you want. There are some recipes in the cookbook.

Cheers guys. How can I change the ‘Home’ link of the breadcrumbs with the module? By default it points to the site home.

You have to set this property depending on the module: http://www.yiiframework.com/doc/api/CBreadcrumbs#homeLink-detail

Something like:




($this->module === null) ? 'Site home link' : $this->module->homeLink;



You’ll need to define “homeLink” property for each module.

How/where do I do all this? Sorry I’m not very familiar with modules.

In your main layout you should have something like:




$this->widget('zii.widgets.CBreadcrumbs', array(

    'homeLink'=>($this->module === null) ? CHtml::link('Home', array('/site/index')) : $this->module->homeLink,

    'separator'=>'<span>&rarr;</span>',

    'links'=>(isset($this->breadcrumbs)) ? $this->breadcrumbs : array(),

));



What in fact I want to do now is have seperate layout files for my front-end application and for my admin module. This should also eliminate the need to add in the IF statement to work out the homeLink (although I assume I will still need to override the default value).

Anyway I know each controller by default has “public $layout=’//layouts/column2’” at the top, I was wondering however can we specify a default layout file at a global level for the module, for example in AdminModule.php? I tried doing $this->layout=‘admin’ but that does not work (I have put admin.php in modules\admin\views\layouts).

CWebModule has a "layout" property: http://www.yiiframework.com/doc/api/CWebModule#layout-detail

I’ve tried public $layout=‘admin’ and public $layout=’//layouts/admin’ but that does not work either. Is this a bug? I haven’t done anything special in my app.

A suggested by the documentation have you removed $layout from the controllers?

http://www.yiiframework.com/doc/api/CWebModule#layout-detail

Yes that’s right, I’ve commented out “public $layout=…” in the controller and it does not make any difference. It just uses the default app layout.

working fine here on multiple projects, takes the layout from modules/admin/views/layouts/main.php




class AdminModule extends CWebModule

{

  public $layout = 'main';

  public function init()

  {

    // this method is called when the module is being created

    // you may place code here to customize the module or the application


    // import the module-level models and components

    $this->setImport(array(

      'admin.models.*',

      'admin.components.*',

    ));

    

     //$this->breadcrumbs['admin'] = 'admin';

  }


  public function beforeControllerAction($controller, $action)

  {

    if(parent::beforeControllerAction($controller, $action))

    {

      // this overwrites everything in the controller

      // this method is called before any module controller action is performed

      // you may place customized code here

      return true;

    }

    else

      return false;

  }


}



No, does not work for me. What version are you using? I’m using version 1.1.3

I can confirm the following:

AdminModule.php has public $layout=‘admin’; at the top.

admin.php exists in \modules\admin\views\layouts

SupplierController.php does not have a public $layout property

The URL I’m accessing is: localhost/mysite/index.php?r=admin/supplier/admin

try this

In AdminModule.php





   public function init() {

 $this->layout='admin';

 $this->setLayoutPath("protected/modules/admin/views/layouts");

    }






No that has not worked either I’m afraid. I’m stumped - what other checks can I do to ensure another setting is not overriding my custom setting?

I’m using 1.1.3.

try forcing it (AdminModule)




  public function beforeControllerAction($controller, $action)

  {

    if(parent::beforeControllerAction($controller, $action))

    {

      // this overwrites everything in the controller

      $controller->layout = 'admin';

      // this method is called before any module controller action is performed

      // you may place customized code here

      return true;

    }

    else

      return false;

  }



Yeah that has worked Sniper.

It’s strange how the ‘official’ method doesn’t work for me…

this works for me




public function init()

  {

    // this method is called when the module is being created

    // you may place code here to customize the module or the application


    // import the module-level models and components

    $this->setImport(array(

      'admin.models.*',

      'admin.components.*',

    ));

    

     $this->layout = 'admin';

  }