Pass additional data with Code Igniter Core Controller - php

I have this core controller in my code igniter app and I want to pass a view variable's to all my views.
My view is loaded in my normal controllers, so I can't load the view again in my Core controller.
So, how am I gonna do that?
I tried using $data['fromCoreVar'] and I tried $fromCoreVar. Neither of both does work which is incredibly well understandable. But, I want it to work.
Whats the solution?
Tnx in advance.

In your core controller, declare a variable:
protected $data;
Then in your core controller, assign data to the variable like:
$this->data['some_index'] = 'some value';
In your child controller you use $data in the same way:
$this->data['other_index'] = 'other value';
and pass it all to your view:
$this->load->view('my_view', $this->data);

Related

issue with passing variable from controller to view in codeigniter

This is my Code Of Controller
$datacc['user_id'] = $this->user_id;
$this->load->view("Templates/header",$this->data);
$this->load->view("Tax/Tax_Search",$datacc);
$this->load->view("Tax/Tax",$datacc);
in this, i want to pass "$datacc" to my both view files (Tax.php And Tax_Search)
but $datacc can't pass to Tax_Search.php file
can anyone help?
Thank You
You cannot call multiple view in one controller function.
This can be done in view file.
I suggest you that you should first create a template file and in that template call your views like this
template.php
$this->load->view("Tax/Tax");
$this->load->view("Tax/Tax_search");
<?php echo $content; ?>
$this->load->view("Tax/footer");
Let me know if you have queries

How to send single variable to view in codeigniter

Hi I want to send single variable to view but I am unable to do it, Please help me.
Controller:
public function src($id=""){
$this->load->view('catsrc', $id);
}
View:
<div class="container">
<?php echo $id ;?>
</div>
Error Getting:
Message: Undefined variable: id
I am not an expert of CodeIgniter but according to official documentation: "Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function"
$this->load->view('catsrc', [ 'id' => $id ]);
was that so hard ?
Happy coding :)
CodeIgniter views read from a $data array. To reference $id in a view, put it in the $data array in your controller
$data = array('id' => 'your_value_here');
and pass $data to the view.
Full documentation on views lives at: https://ellislab.com/codeigniter/user-guide/general/views.html
How CI Classes Pass Information and Control to Each Other
Calling Views
We have seen how the controller calls a view and passes data to it:
First it creates an array of data ($data) to pass to the view; then it loads and calls the view in the same expression:
$this->load->view('testview', $data);
You can call libraries, models, plug-ins, or helpers from within any controller, and models and libraries can also call each other as well as plug-ins and helpers.
However, you can't call one controller from another, or call a controller from a
model or library. There are only two ways that a model or a library can refer back to a controller:
Firstly, it can return data. If the controller assigns a value like this:
$foo = $this->mymodel->myfunction();
and the function is set to return a value, then that value will be passed to the variable $foo inside the controller.
This may not answer your question directly but helps!!

Laravel : Using controller in included view

I have a view that is rendered with its controller. The function that calls the view is linked in my routes. It works fine when directly accessing the route, but obviously my controller is not included when I include it in my template.
How do I use my controller when I include my view?
I'm on Laravel 3.
Right now I have my controller :
public function get_current()
{
// $sales = ...
return View::make('sale.current')->with('sales',$sales);
}
My route (which obv only work on GET /current) :
Route::get('current', 'sale#current');
My master view
#include('sale.current')
Then my sale.current view calls $sales
#foreach($sales as $sale)
Thanks!
So this is the case when you want to call some laravel controller action from view to render another partial view. Although you can find one or another hack around it. However, please note that laravel controllers are not meant for that.
When you encounter this scenario when you want to reuse the same view again but don't want to supply all necessary data again & again in multiple controller actions, it's the time you should explore the Laravel View Composers.
Here is the official documentation link : https://laravel.com/docs/master/views#view-composers
Here is the more detailed version of it :
https://scotch.io/tutorials/sharing-data-between-views-using-laravel-view-composers
This is the standard way of achieving it without any patch work.
Your question is still unclear but I can try to help you. I did a small example with the requirements you gave. I create a route to an action controller as follows:
Route::get('test', 'TestController#test');
In TestController I define the action test as follows:
public function test()
{
return View::make('test.home')->with('data', array('hello', 'world', '!'));
}
According to your asking, you defined a view who includes content from another view (layout) and in that layout you use the data passed for the action controller. I create the views as follows:
// home.blade.php
<h1>Message</h1>
#include('test.test')
and
// test.blade.php
<?php print_r($data); ?>
When I access to "test" I can see print_r output. I don't know if that is what you are doing, but in my case works fine.
I hope that can help you.

How to pass variables to another controller in Laravel

I have a product controller and a header controller. When loading the product controller, I use Route::forward() to load my header controller.
return View::make('frontend.catalogue.product.view')
->with('head', Route::forward("GET", "frontend/page/head"));
I can then echo out the header on the page by doing:
<?php echo $head; ?>
Simple. However, I want to pass information to the header controller which will be defined in the product controller, such as the page title or meta description.
Ideally I would either be able to pass an array to the header, or have an instantiated class somewhere which both route requests have access to.
I've tried instantiating a library in my base controller constructor, however this gets re-instantiated on each Route::forward() request so any properties I set can't be read in the next forward.
The only way I can think of doing this is using session data, but that doesn't seem like it would be the best way to do it.
The header does need to be a controller and not just a view composer, as it deals with it's own models etc.
I know Laravel doesn't do HMVC, which would be the ideal solution, and I know there are bundles for HMVC, but I'm curious how you would approach this the Laravel way.
(Using Laravel 3.2 btw)
Managed to get this working how I want. For others interested:
Instead of using
return View::make('frontend.catalogue.product.view')
->with('head', Route::forward("GET", "frontend/page/head"));
I now do the following in the main request controller:
$document = array(
'meta_title' => 'My Title',
'meta_description' => 'My Description',
);
// Output the view
return View::make('frontend.catalogue.category.view')
->with('head', Controller::call('frontend.page.head#index', array($document)));
and in my frontend/page/head controller, I have the following:
public function action_index($document = array()) {
$meta_title = isset($document['meta_title']) ? $document['meta_title'] : 'Default Title';
return View::make('frontend.page.head')
->with('meta_title', $meta_title);
}
Props to the friendly guys on the Laravel IRC for helping with this

Calling function in Yii controller with renderPartial

Code from controller:
public function actionSomeName($param){
do something here...
$this->renderPartial('_formCalculations', array(
'modelX'=>$modelX,
'modelY'=>$modelY,
));
}
I want to call that function in my view, but it won't work.
Is $this->renderPartial correct ? I think not, because it's only for views? But which function do I have to use then?
$this->renderPartial('Controllername/Somename',array("param"=>"value"));
You can't call the controller/action directly in the view.
You can user render/renderPartial. Render/renderPartial reference
a view file, not a controller action
You could put in your logic in the code as well (in your example, the "do something here..." part) but that is not good MVC!
The other alternative is to use a widget
Surly you can`t use renserPartial individually, but you can do it so:
$body = $this->renderPartial('Controllername/Somename',array("param"=>"value"),true);
In this code you have that view file`s scripts and you can use them anywhere you need.

Categories