I want to change layout for all views inside my controller. So I want to set like this:
class SiteController extends Controller {
public function __construct(){
$this->layout = 'admin';
}
.....
But I am getting the following error:
Call to a member function getUniqueId() on null
By default yii2 uses the main layout as the layout for project and it's controllers, but if you want to use another layout for a controller or change the layout name and use that layout for a controller to have to define a layout property in your controller class and give the name of your layout as the string value of that property.
This will change the layout of that controller to the demanded layout with your chosen name.
Here is the code in your case:
class SiteController extends Controller
{
public $layout = '[Your Layout Name]';
.
.
.
}
P.S: Constructor is the method which runs whenever you create an instance of your class and using it in this case is not logical.
I used init() and worked as expected. I changed my code to:
class SiteController extends Controller {
public function init() {
$this->layout = 'admin';
}
....
Your sitecontroller extends to parent Controller. So you can create constructor in parent controller but not in sitecontroller....
try using
public function init(){
}
in site controller
Related
I am using Codeigniter framework to develop a website. I am currently working on home.php view under the view folder. I need to use UserInfo() function which is inside one of the controllers. Any suggestion how to access that function?
class Welcome extends CI_Controller {
public function UserInfo(){
$this->load->model('model_user');
$data['title'] = 'Users';
$data['users'] = $this->model_user->getUser();
$this->load->view('template/users', $data);
}
}
You cant call controller method inside another controller. Its No Way to do it.
You have two way to resolve this issue
If you want to access the function which place inside the
controller, add that into an model. So by loading model you can call
it.
use redirect('welcome/UserInfo') if you just need to call the function
As You want to call controller function in other controller.In codeigniter App folder core folder exists you make a custom core controller and all other controllers extend with your custom controller
In your Custom Core controller
class CustomCore extends CI_Controller
{
/* ---YOUR FUNCTION IN CUSTOMCORE---- */
public function mycorefunc()
{
//Do something
}
}
and your all other controllers extend with custom core
class YourController extends Customcore
{
function controllerfunction()
{
$this->mycorefunc();// Call corefunction
}
}
I want to pass current path info, i.e controller/action/params, to a widget that uses another model to generate some static HTML contents in the views of the application, including the layout too.
In-order to achieve that goal, I have defined the following controller AppController and I make all of my app controllers extends it instead of yii\web\Controller;
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
class AppController extends Controller
{
public function __construct($id, $module, $config = array()) {
parent::__construct($id, $module, $config);
$this->view->params['path2'] = Yii::$app->controller->id;
}
/*
public function beforeAction($action) {
parent::beforeAction($action);
$this->view->params['path2'] = Yii::$app->controller->id;
} */
}
And, for example, in SiteController:
class SiteController extends AppController
{...
The problem is in any view of any SiteController action, for eaxmple about echo $this->params['path2'] returns blank while I expect it to echo site. In addition, uncommenting beforeAction makes all SiteController actions produces blank pages.
I need any way to allow, globally, set $this->params['path2'] value to be the current controller/action/params regardless what routs rewrites is.
I'm completely confused on how Phalcon PHP renders its views. I want to create a new page called "manager".
From my understanding by creating a controller I can link it to a view. I create a controller called ManagerController.php and then added a view in views/manager/index.volt.
I added a bit of text the volt file to check if it works. When I go to /manager/ nothing shows up.
Am I doing this correct or do I have to assign a view somewhere?
class ManagerController extends ControllerBase
{
public function initialize()
{
$this->tag->setTitle('Files/My Files');
parent::initialize();
}
}
The initialize function on a controller is an event ran after constructing the controller
In order to display view for that controller it is necessary to at least setup an index action
In your you are interested in rendering a route of /manager/ , this will correspond to indexAction
class ManagerController extends ControllerBase
{
public function initialize()
{
$this->tag->setTitle('Files/My Files');
parent::initialize();
}
public function indexAction()
{
// This will now render the view file located inside of
// /views/manager/index.volt
// It is recommended to follow the automatic rendering scheme
// but in case you wanted to render a different view, you can use:
$this->view->pick('manager/index');
// http://docs.phalconphp.com/en/latest/reference/views.html#picking-views
}
// If however, you are looking to render the route /manager/new/
// you will create a corresponding action on the controller with RouteNameAction:
public function newAction()
{
//Renders the route /manager/new
//Automatically picks the view /views/manager/new.volt
}
}
I am trying to figure a way to call a plugin during predispatch, but I am having trouble calling it last.
Basically I need to call this after all controller predispatch, before the action method is called.
Is this possible?
I tried calling the plugin lastly by passing a high value, but I know this is wrong. I am not looking to call this on the postDispatch.
$front->registerPlugin(new Plugin_Acl, 1000);
class Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(...)
{
// ... DO STUFF before any action method
}
}
The standard Zend_Controller_Action has an empty preDispatch() method which is called after preDispatch() on all front-controller plugins (which occurs before the controller is even instantiated) and then on all attached action-helpers, right before the action method itself. See the dispatch() methods on Zend_Controller_Front, Zend_Controller_Dispatcher_Standard, and Zend_Controller_Action for the flow.
So, if you want something to run at that point on all controllers, then you could create a base controller with a preDispatch() method containing the code you want to run.
I am doing that in dispatchLoopStartup()
class My_Plugins_Front extends Zend_Controller_Plugin_Abstract {
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request){
// Register ACL plugin if is admin controller (for eg.)
if($request->getControllerName() == 'admin'){
/**
* Set up your ACL
*/
}
// You can setting variable to see in all view files
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
$view->test = 'test';
}
public function dispatchLoopShutdown(){}
}
calling in predispatch seems to work for my acl:
class Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request) {
parent::preDispatch($request);
//do some stuff
}
Then it instantiated in the application.ini with the line:
resources.frontController.plugins.acl = "Controller_Plugin_Acl"
Hope this helps some...
Is it possible to call the member function of another controller in zend framework, if yes then how?
<?php
class FirstController extends Zend_Controller_Action {
public function indexAction() {
// general action
}
public function memberFunction() {
// a resuable function
}
}
Here's another controller
<?php
class SecondController extends Zend_Controller_Action {
public indexAction() {
// here i need to call memberFunction() of FirstController
}
}
Please explain how i can access memberFunction() from second controller.
Solution
Better idea is to define a AppController and make all usual controllers to extend AppController which further extends Zend_Controller_Action.
class AppController extends Zend_Controller_Action {
public function memberFunction() {
// a resuable function
}
}
class FirstController extends AppController {
public function indexAction() {
// call function from any child class
$this->memberFunction();
}
}
Now memberFunction can be invoked from controllers extending AppController as a rule of simple inheritance.
Controllers aren't designed to be used in that way. If you want to execute an action of the other controller after your current controller, use the _forward() method:
// Invokes SecondController::otherActionAction() after the current action has been finished.
$this->_forward('other-action', 'second');
Note that this only works for action methods (“memberAction”), not arbitrary member functions!
If SecondController::memberFunction() does something that is needed across multiple controllers, put that code in a action helper or library class, so that both controllers can access the shared functionality without having to depend on each other.
You should consider factoring out the code into either an action helper or to your model so that it can be called from both controllers that need it.
Regards,
Rob...
I would suggest you to follow DRY and move those functions to common library place. For example create in library folder
My/Util/
and file
CommonFunctions.php
then call your class
My_Util_CommonFunctions
and define your methods
Now you can call them from any place in the code using your new namespace which you have to register.
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace(array('My_'));
in any controller you can call your custom methods by using:
My_Util_CustomFunctions::yourCustomMethod($params);