I am trying to write a custom extension for yii framework and I cannot access controller which is stored in extensions/controllers folder. I have searched a lot but I did not find any hint for that. This is the structure
protected
-- extensions
-- XYZ (Extension's name)
-- assets
-- controllers
-- XYZController.php
-- models
-- XYZModel.php
-- widgets
-- views
-- form.php
-- XYZWidget.php
I have done all adjustments needed to load extension and currently I am able to display the the form from a views folder by using $this->renderInternal('views/form.php'). So the extension is loaded successfully. The problem appears when I try to access controller inside extensions folder.
Usually when one need to access for example index method inside protected/controllers/SiteController.php then the URL is http://project.com/index.php/site/index" Therefore, what should I write in form action in order to access any method in /extensions/controllers/XYZController.php from a form.
I added this chunk of code in main.php like it was written in various recommendations
'controllerMap'=>array(
'XYZ'=>array(
'class'=> 'XYZ.controllers.XYZController',
),
),
config/main.php file
Yii::setPathOfAlias('XYZPath', realpath('protected') . '/extensions/AQ');
return array(
-------
-------
'preload'=>array('log', 'XYZ'),
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
'application.extensions.XYZ.controllers.*',
'application.extensions.XYZ.models.*',
'application.extensions.XYZ.widgets.*',
),
'controllerMap'=>array(
'XYZ'=>array(
//'class'=> 'XYZPath.controllers.XYZController',
'class'=> 'extensions/XYZ/controllers/XYZController',
),
),
-------------
-------------
)
Kindly asking to give me any hint in this situaion. Thanks in advance.
I am using minScript extension in many of my projects and I know that this extension loads a controller from its own directory.
You are on the right path but you're referencing the controller in a wrong way.
minScript extension does it like this:
As you see, no imports required. I think "ext." is already an alias to extensions directory, you could also have written: "application.extensions." instead.
Link to docs on controllerMap config. parameter: http://www.yiiframework.com/doc/api/1.1/CWebApplication#controllerMap-detail
You should always inspect the framework's code if you are not sure what kind of parameter to pass somewhere.
So based on this, you should end up with something like:
And you would access it by /index.php?r=XYZ/index assuming you have a "index" action. Also keep in mind that on non-Windows environment by default Yii urls are case sensitive. so a route like "xyz/index" would not work if the controller is named XYZController.
And no need for the import/path alias. At least not for the controller. You might need imports to load the rest of your extension's files, such as models, widgets, and so on, but as Aleksei pointed Yii doesn't import recursively so you should import one by one all the directories of your extension that contain classes that you want to have them auto-loaded.
Example importing all stuff from a extension:
One last thing, I don't know what your extension does but if you're going to have a lot of controllers you might consider developing a module instead.
Extensions are just plain directories containing some tool that you can import into the application and use. But if your extension is a "mini" application, with many controllers, it would be easier to manage as a module because in a module all controllers are loaded automatically when the module is loaded and its structure is very similar to a full Yii application.
If you need a module to start with, I think Gii code generation tool can make one for you.