Different interface layout for different level users in yii - php

I'm new to Yii. I'm developing a system with YII framework in PHP. How can I have a different layout for different module? I want the module A to have interface A, module B with interface B. But what I have know is that the interface login is the same for all module login. Can someone give me a light?
Update:
I found one way which is to include the:
$this->layout = $layout;
on the action function inside the controller before rendering the page. However, I found that it's not that efficient as on every action I need to repeat the line. Is there a way where we can do the setting on the config/main.php page? probably on this part:
'modules'=>array(
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'123',
'generatorPaths' => array('bootstrap.gii'),
),
'admin',
'consultant',
'client',
),

You can set variables for your module in your config like this:
'modules'=>array(
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'123',
'generatorPaths' => array('bootstrap.gii'),
),
'admin' => array(
'layout' => 'your_layout' //The layout for this module
),
'consultant',
'client',
),
This way you can implement a default layout for every single module. Without having to add controller methods or variables.
For more info see the docs: here and here

try this:
class YourController extends Controller {
public $layout = 'your_layout';
}

Related

link a bootstrap to a certain module

I have difficulty in connecting the bootstrap to a module. The bootstrap location is at: mainFolder/protected/extensions/bootstrap/theme/abound
protected/config/main.php
Yii::setPathOfAlias('bootstrap', dirname(__FILE__).'/../extensions/bootstrap');
'modules'=>array(
'admin',
'consultant'=>array(
'preload'=>array('bootstrap'),
'components'>array(
'bootstrap'=>array(
'class'=>'bootstrap.theme.abound'
),
),
),
'candidate',
),
protected/modules/consultant/ConsultantModule.php
class ConsultantModule extends CWebModule
{
public function init()
{
$this->setImport(array(
'consultant.models.*',
'consultant.components.*',
));
Yii::app()->getComponent('bootstrap');
}
}
structure of the folder:
mainFolder
protected
config
**main.php**
extensions
bootstrap
assets
components
form
gii
theme
**abound**
widgets
modules
consultant
controllers
models
views
**ConsultantModule.php**
Error that I get when I run the code:
Property "ConsultantModule.0" is not defined.
Is it because the properties inside the module's consultant is wrong or the way I place the bootstrap is not correct? I can't figure out the problem.

Phalcon Router and Loader for subfolder structure getting bigger. How to set up?

I have a quite big project that I need to program. I used to code with CodeIgniter but since they stopped maintaining the framework, I decided to switch to another. I chose Phalcon framework. The folder structure of the application that I want to implement is next:
app/
controllers/
admin/
users/
UsersController.php
UserGroupsController.php
dashboard/
system/
another_subfolder/
AnotherSubFolderController.php
production/
settings/
SettingsController.php
dashboard/
flow/
another_subfolder/
AnotherSubFolderController.php
website1/
customers/
CustomersController.php
CompaniesController.php
another_subfolder .... /
models/
sub_folder_structure/ // The same as controllers
This is just example of the folder structure of the application. There will be quite a lot of folders and sub-folders in this project to make it manageable.
From my understanding I need to register all namespaces at the loader for each sub-folder, so that Phalcon knows where to search and load the class.
//Register an autoloader
$loader = new \Phalcon\Loader();
$loader->registerNamespaces(array(
// Main
'Controllers' =>'app/controllers/',
'Models' =>'app/models/',
// Admin Routing
'Controllers\Admin'=>'app/controllers/admin',
'Models\Admin'=>'app/models/admin',
'Controllers\Admin'=>'app/controllers/admin',
'Models\Admin'=>'app/models/admin',
'Controllers\Admin\Users'=>'app/controllers/admin/users',
'Models\Admin\Users'=>'app/models/admin/users'
))->register();
The loader will look quite big.
Then I have to set-up the router so that requests redirected to the right controller. Right now I have next:
// Setup Router
$di->set('router', function(){
$router = new \Phalcon\Mvc\Router(false);
$router->removeExtraSlashes(true);
$router->add('/', array(
'namespace' => 'Controllers',
'controller' => "login"
));
$router->add('/:controller/:action/', array(
'namespace' => 'Controllers',
'controller' => 1,
'action' =>2
));
$router->add('/admin/:controller/:action/', array(
'namespace' => 'Controllers\Admin',
'controller' => 1,
'action' =>2
));
$router->add('/admin/users/:controller/:action/', array(
'namespace' => 'Controllers\Admin\Users',
'controller' => 1,
'action' =>2
));
return $router;
});
That will be also very big if I need to manually setup router for each sub-folder and namespace.
So the questions that I have are this:
Is there any way to make a loader prettier and smaller? As it will grow bigger.
How can I setup router so that I don't need to enter all of the namespaces and combinations of subfolders? Is there any way to make it smaller? May be modify dispatcher or any other class? Or is there a way I can set a path variable in the router to the location of the controller?
I have researched Phalcon documentation but could not find the way to do that.
Any help and suggestions are very much appreciated.
Thanks
Ad. 1. You can change your namespaces to be more PSR-0 (in my opinion), so I would make:
app
controllers
Admin
Users
UsersController.php
The you can register one namespace Admin or any other. Then you need to register only the top most namespace to work (keep in mind that your UsersController must have namespace Admin\Users\UsersController; to work). Thena autoloader should have only:
$loader
->registerDirs(
array(
// It's taken from my config so path may be different
__DIR__ . '/../../app/controllers/'
// other namespaces here (like for models)
)
);
I'm using registerDirs so I only point loader to the folder in which some namespace exists.
Ad. 2.
For this you can use groups of routes so you can pass a namespace as a parameter for config array of constructor and then do the repeative task in one place. Then just create new instances with different parameters;
$router->addGroup(new MahGroup(array('namespace' => 'Mah\\Controller'));
So inside MahGroup class could be:
class MahGroup extends Phalcon\Mvc\Router\Group {
public function _construct($config = array()) {
$this->setPrefix('/' . $config['perfix']);
$router->add('/:controller/:action/', array(
'namespace' => $config['namespace'],
'controller' => 1,
'action' => 2
));
// etc...
}
}
And then configuring routes:
$router->addGroup( new MahGroup(array('prefix' => 'mah-namespace', 'namespace' => 'Mah\\Namespace' )) );
$router->addGroup( new MahGroup(array('prefix' => 'mah-other-namespace', 'namespace' => 'Mah\\Other\\Namespace' )) );
But given examples for second question are just what could be done. I usually create Group class for each namespace and then declare some routes that my app uses since I'm not using english names for routes and I need rewriting polish urls to controllers which have also some namespaces.

Changing CSS file in YII app doesn't work

I am trying to change the styling of the gridview, tableview, & detailview. I found something saying that I should change the config/main.php file to this:
...
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'bootstrap'=>array(
'class'=>'bootstrap.components.Bootstrap',
),
'widgetFactory'=>array(
'widgets'=>array(
'CGridView'=>array(
'cssFile' => Yii::app()->request->baseUrl.'/css/table_and_grid.css',
),
),
),
...
I have removed the assets folder that is generated by the app, but that didn't help. When I load the view, I can see that the css sheet is being loaded into the header of the page, but none of the styling is working. Why? How do I fix?
I haven't seen anything about changing the style of CGridView in main config file(main.php), But you can customize CGridView styles with bellow parameters:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'SOME ID',
'dataProvider'=>$YOUR_DATA_PROVIDET,
'cssFile'=>'...',
'baseScriptUrl'=>'...',
'filterCssClass'=>'...',
'itemsCssClass'=>'...',
'pagerCssClass'=>'...',
'rowCssClass'=>'...',
'summaryCssClass'=>'...',
));
You can change ... with your own.
for more information you can check CGridView's Official document on the following link:
CGridView

How to change Yii Error Handler on the fly?

In Yii's config.php we have this statement which declares which Controller is the default and only-one Error-Handler within the application:
'errorHandler' => array(
'errorAction' => 'site/error',
),
So, I need to have an actionError() under my SiteController, to get the errors previewed in my site, but this is not what I really need.
I am trying to change the Yii::app()->errorHandler->errorAction on the fly, throughout my custom-controllers who extend the base CController (Yii's base controller).
Till now, I have tried something like this:
<?php
class AdminController extends CController {
public $layout = '//layouts/admin';
public function init() {
parent::init();
Yii::app()->errorHandler->errorAction = '/admin/error';
}
}
But gives no results, nor hope. Note that I also have this URL configuration:
'/admin' => '/admin/home',
'/admin/<controller:\w+>' => '/admin/<controller>',
'/admin/<controller:\w+>/<action:\w+>/<id:\d+>' => '/admin/<controller>/<action>',
'/admin/<controller:\w+>/<action:\w+>' => '/admin/<controller>/<action>',
And this means I have a whole Controllers-Views group named admin, and they are stored in following directories:
protected/controllers/admin
protected/views/admin
So by that logic, I have ErrorController in both: admin and controllers root, and by the same structure in the views directory.
That's what I have tried, and I really appreciate help, so thank you all in advance!
You should try this :
Yii::app()->setComponents(array(
'errorHandler'=>array(
'errorAction'=>'/admin/error'
)
));
http://www.yiiframework.com/doc/api/1.1/CModule#setComponents-detail

How to extend moduleManager in Zend Framework 2?

I want to extend standard Zend\ModuleManager\ModuleManager, is it possible?
For example I want to load modules list from database and I want to add some methods for working with modules.
If I set factory to serviceManager:
'service_manager' => array(
'factories' => array(
'moduleManager' => 'Path/To/My/ModuleManager',
),
),
There is error "A service by the name or alias "modulemanager" already exists and cannot be overridden, please use an alternate name"
The module manager in Zend Framework 2 is created via a service factory class. ModuleManager is mapping to Zend\Mvc\Service\ModuleManagerFactory. I advice you to have a look on this ModuleManagerFactory and see what object are injected in the module manager on createService().
If you want to extend and use your own module manager you must create a class that extends ModuleManager but also create a service manager factory that overwrites the Zend\Mvc\Service\ModuleManagerFactory. You are on the right way with the following code but it is important to put this code in the /config/application.config.php file because this is the config file that Zend\Mvc uses to create the main services.
// config/application.config.php
'service_manager' => array(
'factories' => array(
'ModuleManager' => 'Path/To/My/ModuleManagerFactory', // <-- Path to MM factory
),
),
The link below will give you good information about what default services are run with \Zend\Mvc and how and where this is happening:
https://zf2.readthedocs.org/en/latest/modules/zend.mvc.services.html
Hope this helps, feedback will be appreciated :)
Stoyan

Categories