I am trying to access a module's model from another module ,
ex:
protected/modules/module1/DefaultController
protected/modules/module2/DefaultController
i want to access module1 models from module2 ,
i tried
Yii::app()->getModule('module1');
$m = new module1;
it showing the error
include(module1.php): failed to open stream: No such file or directory
I believe that you did not declare your module in config (main.php):
'modules'=>array(
...
'module2'=>array(
...
),
),
Also check whether you have named the class module. It should be Module1Module.
And I can see that you do not have a the correct directory structure of the module. Controllers should be in the directory сontrollers:
/protected/modules/module1
Module1Module.php
controllers/
defaultController.php
views/
index.php
Related
I have 2 websites projects on the main directory inside /var/www/ directory
what I want to do is creating controller with it's views to be shared between the 2 project, not repeat the same controller on both projects,
now I create it ex. myController.php inside host A
How can I access the controller from the second host B ?
and render myaction function?
the url rules in the main.php config file
'newpage/list'=>'myController/myaction',
Edit :: I'm using this advanced template
**DIRECTORY STRUCTURE**
common
config/ contains shared configurations
mail/ contains view files for e-mails
models/ contains model classes used in both backend and frontend
tests/ contains tests for common classes
console
config/ contains console configurations
controllers/ contains console controllers (commands)
migrations/ contains database migrations
models/ contains console-specific model classes
runtime/ contains files generated during runtime
backend
assets/ contains application assets such as JavaScript and CSS
config/ contains backend configurations
controllers/ contains Web controller classes
models/ contains backend-specific model classes
runtime/ contains files generated during runtime
tests/ contains tests for backend application
views/ contains view files for the Web application
web/ contains the entry script and Web resources
frontend
assets/ contains application assets such as JavaScript and CSS
config/ contains frontend configurations
controllers/ contains Web controller classes
models/ contains frontend-specific model classes
runtime/ contains files generated during runtime
tests/ contains tests for frontend application
views/ contains view files for the Web application
web/ contains the entry script and Web resources
widgets/ contains frontend widgets
vendor/ contains dependent 3rd-party packages
environments/ contains environment-based overrides
Question is : How I can access the front end controllers in front end directory from backend rules ?
Frontend and Backed are two different modules. When they are bootstrapping from index.php they behave like two individual projects. So, you cannot route from frontend to backed or vise versa using urlManager of Yii.
May be you can maintain some params in common/params where you can configure absolute Url.
Actually you can reuse frontend controller classes in backend - you can use controllerMap property of application or module to define custom controller classes. For example if you add something like this to your backend config:
'controllerMap' => [
'mycontroller' => 'frontend\controllers\SomeController',
],
Then frontend\controllers\SomeController will act like it would be backend\controllers\MycontrollerController - backend.local/mycontroller will use the same controller as frontend.local/some, but with different contexts (and probably layouts).
You can even use controllerNamespace to load all controllers from given namespace. For example create separate module in backend:
namespace backend\modules;
class FrontendModule extends \yii\base\Module {
public $controllerNamespace = 'frontend\controllers';
}
Then this module will use all frontend controllers at backend context. backend.local/frontend/some will use frontend\controllers\SomeController.
I downloaded HMVC files from here and set up my CI 3 installation using these file
Place the MX folder in application/third_party folder
Place the files in application/core MY_Loader & MY_Router
$config['modules_locations'] = array(APPPATH.'modules/' => '../modules/',);
Created a folder module in application. Inside module created welcome/controller and welcome/view
In welcome/controller I copied the default welcome controller and in welcome/view welcome_message.
I deleted both files from application/controller and application/view.
Now I am getting 404 error.
You change name of folder "module" to "modules".
And in modules folder:
You must rename controller and view folder to controllers and views.
Hope this help :)
Same as the title, i want to create config file for each module in yii, example:
test/
TestModule.php the module class file
components/ containing reusable user components
config/ containing config class files
main.php the config file <===
views/ containing view files for widgets
controllers/ containing controller class files
DefaultController.php the default controller class file
models/ containing model class files
views/ containing controller view and layout files
layouts/ containing layout view files
default/ containing view files for DefaultController
index.php
What is the solution? Can somebody help me?
You can modify your main config and point to config file for each module:
return array(
......
modules => array (
require('path./to/configfile1.php'); //first module config
require('path/to/configfile2.php'); //second module config
),
.......
)
and in your module config file
return array(
'class' => 'alias.to.class',
'param1' => 'val1'
.......
)
I wrote a class CronTab_Manager to generating and managing cron jobs whith PHP. Now I want to access it everywhere in my project.
In my config file I set path of alias like below:
$cronTab = dirname(dirname(dirname(__FILE__))).'/extensions/CronTab_Manager';
Yii::setPathOfAlias('cronTab',$cronTab);
And also import that class in this way:
'import'=>array(
'application.models.*',
'application.components.*',
'cronTab.*',
),
My class isn't extended from other classes, and when I want to create an object of this class I receive this error:
include(CronTab_Manager.php): failed to open stream: No such file or directory
Any suggestion will be appreciated.
Thanks in advance.
you can put your class file inside models folder or components folder.
if it is as extension then
'import'=>array(
'application.models.*',
'application.components.*',
'ext.cronTab.*',
),
Set path like this:
Yii::setPathOfAlias('cronTab','ext.CronTab_Manager');
So this is my project directory structure:
application/
layouts/
scripts/
default.phtml
partials/
partial.phtml
modules/
default/
controllers/
models/
forms/
views/
scripts/
public/
In the default.phtml layotu I'm trying to include a partial like this:
<?php echo $this->partial('partials/partial.phtml', array()); ?>
Which is getting me this error:
script 'partials/partial.phtml' not found in path (...)
Does that mean partials can be included only from within view scripts? I could put the partial inside modules/default/views directory but that seems wrong because in case there are more modules the same partial file would repeat itself multiple times.
partials will be loaded from the views/scripts directory not from the layouts/scripts directory also when beeing called from the layout viewscriopt.
if you really need to have your partials in the layout folder you need to configure a new view object with the scriptPath pointing to your layouts/scripts dir. instead you can maybe find an existing view object in the layout internals which already has this path set.
then simply call the partial viewhelper on this view object.