Global Query Data - php

There's a part of a code (query) that will be required in all controllers, they will be passed into views for display.
Can I know is there anyway to declare them in just a single file so that I can reference them directly from my view? Without declaring them in each controller's _construct.
I'm using codeigniter3, here's a sample code:
MainController.php
public function index(){
$data['userCampaign'] = $this->Usermodel->getCampaign();
}

Create default controller in your project which extends CI_Controller and your all controller extends new controller and in __construct(); function of your new controller you can add this code.

Declare this function as protected in parent controller class.

No. I don't know there is method like that.
You want call the function in __construct() or you have to declare function in controller and call it back. $this->check_session()

Related

How to run code before all my methods inside controller in Laravel 4?

I have Content controller with REST methods (index..create..store..) and i want to run some code before any of those methods run.
what i am trying to do is to set var for my layout with some data that is relevant to all my methods within Content controller:
$this->layout->myvar = 'some-data';
I tried to do something like that:
class ContentController extends BaseController {
function __construct() {
$this->layout->myvar= 'some-data';
}
..
but it doesn't seems to work.
i get "Attempt to assign property of non-object" error.
Laravel 5.1+
This has been deprecated in favour of Middleware.
Laravel 4
You could set the beforeFilter like this:
class ContentController extends BaseController {
function __construct() {
// this function will run before every action in the controller
$this->beforeFilter(function()
{
// this will make the variable $myvar available in your view
$this->layout->with('myvar', 'some-data');
});
}
}
try share in app/routes.php
View::share('variable_name', 'value');
ex:
View::share('name', 'Steve');
will share variable with its value across all views

how can I store $data array for all methods in a controller

I have some data array which I need for all method's in a controller.
$data['project_ongoing_res_limit']=$this->admin_model->show_project_ongoing_residential_limit();
$data['project_ongoing_com_limit']=$this->admin_model->show_project_ongoing_commercial_limit();
$data['project_upcoming_res_limit']=$this->admin_model->show_project_upcoming_residential_limit();
$data['project_upcoming_com_limit']=$this->admin_model->show_project_upcoming_commercial_limit();
$data['project_completed_res_limit']=$this->admin_model->show_project_completed_residential_limit();
$data['project_completed_com_limit']=$this->admin_model->show_project_completed_commercial_limit();
Problem is I cant DRY this. so I have paste this $data array in each method.
I have a view page for this. so when I load this view , I have to
load above $data array each time/method. this is disgusting when controller
methods are too much.
I want 1 piece of this code like constructor. How can I do this.
you can use traits for this.
Define your methods in a trait, and then use the trait in the controllers.
You can make one helper class in which make a function and put your above code in it but make sure you can't access model using $this so, you need to create CI instance and than access it. after that in your controller in construct method you just need to call this function but don't forget to load the helper class and store it in a variable and pass it along with view.
Just create private data variable in your controller class. Than set your data in constructor. Now you can access your data in any method you want.
class Pages extends CI_Controller {
// ...
private $data;
// ...
public function __construct() {
parent::_construct();
$this->data = array();
$this->data['project_ongoing_res_limit']=$this->admin_model->show_project_ongoing_residential_limit();
$this->data['project_ongoing_com_limit']=$this->admin_model->show_project_ongoing_commercial_limit();
$this->data['project_upcoming_res_limit']=$this->admin_model->show_project_upcoming_residential_limit();
$this->data['project_upcoming_com_limit']=$this->admin_model->show_project_upcoming_commercial_limit();
$this->data['project_completed_res_limit']=$this->admin_model->show_project_completed_residential_limit();
$this->data['project_completed_com_limit']=$this->admin_model->show_project_completed_commercial_limit();
}
// ...
}

Create constructor method in controller in Yii

I have just started to learn Yii, where I have created one PostController Controller. In this controller I am having one requirement of using Sessions.
So I have created one constructor method and its code is as follows
public $session;
public function __construct() {
$this->session = new CHttpSession;
$this->session->open();
}
But after creating this constructor the controller was not working and gives error. And after deleting this code my controller was working perfectly. I have written this code inside constructor to not initialize the Session in each method for actionCreate and actionUpdate.
So my question is how can we create constructor in Yii?
Thanks
You simply forgot to call parent constructor :
public function __construct()
{
.....
parent::__construct();
}
You could use beforeAction instead of overriding __construct.
And Sergey is right, by default Yii will start session (autoStart), you just have to use Yii::app()->session, e.g. :
Yii::app()->session['var'] = 'value';
public function __construct()
{
parent::__construct($this->id, $this->module);
}
I use init() for that, but found what people think __construct is better.

Autoload function and get variables in codeIgniter

I have this question: how to load function in codeIgniter in every page and get data or variables from it to show in view?
I have many controllers and each one has many function. Each function load master view as a template and load its own view. Master view has dynamic data I push it from database.
So I need in master view to show some data from database automatically. How to do it?
You can use this. change it according to your
function index()
{
$data['list'] = $this->some_model->show_data();
$data1['content'] = $this->load->view('some_view',$data,true);
$this->load->view('template',$data1);
}
content is in your main template where u will pass your data to show in template
I am not sure what exactly you ask but if you want a function to be loaded automatically to any controller
then create a Mycontroller.php in application/core
class Mycontroller extends CI_Controller
{
function __construct()
{
parent::__construct();
// here goes your function
}
}
and then extend every controller you have to Mycontroller instead of CI_Controller
if you want to use your function inside views. use CI Helpers
Codeigniter Helpers
create your helper function
function get_data() {
// and use CI instance
$CI = & get_instance();
// now you can call your models, libraries. etc. in the helper
$data = $CI->your_model->your_model_funcion();
return $data;
}
and you can call your helper function in controllers, views, models. etc...
get_data();

Laravel how to include a partial in the base controller

In my laravel application I am using a Base_Controller class and then extend this class in other controller.
In my app there is a variable which I need to use in all my controller and templates.
This is why I tried to use in my base controller.
$this->layout->myVar = 'stuff'
But when I try use $myVar in my view I am getting an error:
Creating default object from empty value
My base class constructor is something like this:
public function __constructor()
{
parent::__construct();
$this->layout->menu = 'stuff';
}
Does anyone have any idea on what is the best way to approach this?
You must have $this->layout defined in your controller class or else $this->layout must be assigned an instance of View::make().
$this->layout->menu may also require having the #section('menu') #endsection in it.

Categories