Create config file for each module Yii ver 1 - php

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'
.......
)

Related

Yii2 How can I route the current Url to controller outside the root path of the current project?

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.

Codeigniter 3 HMVC wiredesignz showing 404 error

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 :)

access a module1 DefaultController from module2 DefaultController yii

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

Yii folder structure and global configuration for n applications

I'm building a folder structure and global configuration to allow me to create applications on Yii framework where all applications could share common codes, extensions or modules.
The idea is, each application would have the business level for that application, but, everything that can be shared between applications, we want to use as a common repository (models, extensions, widgets, etc)
For example, we want to share the Auth extension to control the system permission, but I want to 'install' Auth under the common folder instead under each application.
Below, the architecture that I would like to build:
I found the YiiBoilerplate as something similar, but they don'y configure Yii in such way that we want to.
The folder structure desire is:
common/
css/
images/
protected/
commands/
components/
config/
controllers/
extensions/
models/
views/
Application1/
css/
images/
protected/
commands/
components/
config/
controllers/
extensions/
models/
views/
So, let's go for some points:
If my user is under the application X (www.applicationx.com) and the login page is a 'common' structure I want to display to user something like this: www.applicationx.com/index.php?r=user/login.
I want to be able to 'navigate' between the application folder and the common folder easily.
I do not want to use Theme because I have different applications with different visual requirements and behaviors.
Any thoughts about this? Any good advices? Thanks in advance!
Well, I found some way of how to do it.
First of all, my folders structure, looks similar as it:
common/
css/
images/
js/
extensions/
bootstrap/
auth/
protected/
/* Yii Default directory folder*/
Application1/
css/
images/
js/
protected/
/* Yii Default directory folder*/
Config File Under Common Folder
return array(
'import'=>array(
'common.components.*',
'common.models.*',
),
'modules' => array(
'auth',
),
'components' => array(
'authManager' => array(
'behaviors' => array(
'auth' => array(
'class' => 'common.modules.auth.components.AuthBehavior',
'admins'=>array('admin', 'foo', 'bar'),
),
),
),
'user' => array(
'class' => 'common.modules.auth.components.AuthWebUser',
),
'bootstrap'=>array(
'class'=>'common2.extensions.bootstrap.components.Bootstrap',
),
),
);
Login
The login is under 'common', so, to redirect to the login page, I just call the SiteController.php and I implented there the login process, giving for the user the ability to select the application that he wants to get in.
After the login, I redirect the user to the application address:
$this->redirect(Yii::app()->request->getBaseUrl(true) . "/../" .$App. "/" . "");
Config File Under Application Folder
under the application, the index.php file has the alias for the common directory, is the way that they can still 'talking'.
Yii::setPathOfAlias('common', dirname(__FILE__) . $directory);
If you want to do something similar and are finding hard to understand, let me know, I will put here more information, if you need to.

Partial in layout error

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.

Categories