How to use Yii controller located in folder rather than default `protected/controller`?

StackOverflow https://stackoverflow.com/questions/7976977

  •  19-02-2021
  •  | 
  •  

سؤال

In Yii, when access to <app-host>/index.php?r=<mycontroller>/<myaction>, the framework will start to run code in protected/controller/<MyController>Controller.php

I want to that code to be located in another folder, says protected/anotherFolder, while other controllers remain as-is. How to do that?

Regards

هل كانت مفيدة؟

المحلول 2

Thanks to mdomba on yii forum at this post, I found the answer using CWebApplication::controllerMap - in the loading state of the application we call

Yii::app()->controllerMap['yourControllerName']='path.alias.to.your.controller.file.without.dotPHP';

You can use controllerMap - http://www.yiiframew...ollerMap-detail

نصائح أخرى

Open up <app-host>/index.php, edit to

//...
require_once($yii);
$app = Yii::createWebApplication($config);
$app->setControllerPath('protected/anotherFolder');
$app->run();

If I got you, you want to split the web logic into different "folders", or (in a more yii-ly way) modules. For instance, to have all the administrating stuff into another place and get to this using r=admin/users, for instance

If you have your gii manager activated, go to /index.php?r=gii, and create a module. That's it. You can then create controllers inside protected/modules/<module-name>/controllers/ and call them using that path. Of course, views are also stored inside that

You can add to config file

'controllerPath' => 'your_new_controller_path',

The same with view path

'viewPath' => 'path_to_template_folder',
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top