Let say we have something like this in a AppServiceProvider
$page = [
'title' => 'Page Name',
'info' => 'Content Here'
];
view()->share('page', $page);
In view:
<h1>{{$page['title']}}</h1>
<p>{{$page['info']}}</p>
If I want to overwrite $page['title'] in method controller, I have tried like this:
public function index()
{
$page['title'] = "overwrite title only";
return view('index', compact('page'));
}
Problem is $page['info'] will not longer be available, it will not exist in view. How can I overcome this situation?
The simplest solution could be to replace in your Controller:
$page['title'] = "overwrite title only";
with:
$page = array_merge(
view()->shared('page'),
['title' => "overwrite title only"]
);
view()->shared() method returns the shared variable, then we can override some array elements and pass the overridden array to the view.
Related
I need to use the function in my view/template page.
My code:
public function getAppNumber($id = null){
$aPPLICATIONINFO = $this->AppEstLto->APPLICATIONINFO->find('all', [
'fields'=>['application_number'],
'conditions'=>['application_id'=>$id],
'limit' => 200
]);
foreach($aPPLICATIONINFO as $aPPLICATIONINFO){
$aPPLICATIONINFOx = $aPPLICATIONINFO;
}
return $aPPLICATIONINFOx;
}
You can use set() to use the function variables in your view as given in cookbook:https://book.cakephp.org/3/en/views.html#setting-view-variables
public function get_app_number($id = null){
$applicationInfo = $this->AppEstLto->APPLICATIONINFO->find('all',
[
'fields'=>['application_number'],
'conditions'=>['application_id'=>$id],
'limit' => 200
]);
//Create an array
$applicationArray = new array();
//Store all results in array
foreach($applicationInfo as $application){
$applicationArray = $application;
}
// Pass the array to view
$this->set(compact('applicationArray'));
}
Now, you can use it in your view:
get_app_number.ctp:
<?php
foreach($applicationArray as $application)
{
echo $application['application_number'];
}
?>
You should be doing something like this in your routes.php file inside your Config folder:
Router::connect('/get-app-number', array('controller' => 'yourController', 'action' => 'get_app_number'));
That way you will be able to connect the url that will be used for your view.
Your action corresponds and sends data to your view.
The action is the function that is inside your controller in which you developed for setting the data and variables.
The example of the slug which would be generated:
http://localhost/get-app-number
I have two different queries which are saved in two variables.I want to pass the variables to view page from controller.
public function getApprovalList(){
// $users = select query..
// $request = select query..
return view('travelerHome',['users'=>$users,'request'=>$request]);
}
solution:
controller
return view('travelerHome',['users'=>$users,'requestList'=>$request]);
view
#foreach ($requestList as $req)
{{$req->traveler_name }}
#endforeach
return view('travelerHome')->with(array('users'=>$users,'request'=>$request));
You can use return view('travelerHome', compact('users', 'request')); too
Do like this
return view('travelerHome')->with('users'=>$users)->with('request'=>$request);
Code with you have written that is correct, you need to remove the comment only, I am also using the same method for pass the variable to View. Here is the example.
return view('admin.product.product.edit', ['product' => $product,
'attribute_set' => $productAttributes,
'category' => $cates,
'images' => $images,
'status' => $status,
'countries' => $countries,
'crane_manufacture' => $crane_manufacture,
'product_category' => $productCategory]);
}
And it's also working
So, in your code you need to
public function getApprovalList(){
$users = 'select query..';
$request = 'select query..';
return view('travelerHome',['users'=>$users,'request'=>$request]);
}
I am using codeigniter I have an edit page which shows me all information of a vacancy.
The (Vacancy) controller method to load this view looks like this, it makes sure all data is preloaded.
public function editVacancy($vacancyid)
{
$this->is_logged_in();
$this->load->model('vacancy/vacancies_model');
// Passing Variables
$data['title'] = 'Titletest';
$data['class'] = 'vacancy';
$orgadminuserid = $this->vacancies_model->getOrgAdminUserId($vacancyid);
if ((!is_null($orgadminuserid)) && ($this->auth_user_id == $orgadminuserid[0]->user_id)) {
$data['vacancyid'] = $vacancyid;
$data['vacancy'] = $this->vacancies_model->get($vacancyid);
$data['test'] = $this->session->flashdata('feedbackdata');
$partials = array('head' => '_master/header/head', 'navigation' => '_master/header/navigation_dashboard', 'content' => 'dashboard/vacancy/edit_vacancy', 'footer' => '_master/footer/footer');
$this->template->load('_master/master', $partials, $data);
}
}
In this view i have different forms for updating different sections.
Every form submit goes to a different method in my 'Vacancy' controller.
public function saveGeneralInfo()
{
$this->is_logged_in();
$this->load->model('vacancy/vacancies_model');
$vacancyid = $this->input->post('vacancyid');
$vacancyUpdateData = $this->vacancies_model->get($vacancyid);
$result = $this->vacancies_model->update($vacancyid, $vacancyUpdateData);
if ($result) {
$feedbackdata = array(
'type' => 'alert-success',
'icon' => 'fa-check-circle',
'title' => 'Success!',
'text' => 'De algemene vacature gegevens zijn geupdate.'
);
$this->session->set_flashdata('feedbackdata', $feedbackdata);
redirect("dashboard/vacancy/editVacancy/" . $vacancyid);
}
}
}
Where I indicated in my code "//HERE ..." is where I would want the feedback message parameter to pass on to my 'main controller method' which loads the view with the prefilled data. (editVacancy).
Is there a clean way to do this?
EDIT:
I tried using flashdata, i updated the code to have the flashdata inserted.
However when i do a var_dump($test); in my view, it remains null.
EDIT 2:
I noticed when i put my $_SESSION in a variable in my editVacancy controller method (which is being redirected to) and var_dump it in my view that this does not contain the ci_vars with the flashdata in.
Instead of using set_header you can use simple redirect function for it.
redirect("dashboard/vacancy/editVacancy/".$vacancyid);
I have the following method in a controller
public function artikel(){
$breadcrumb = array(
'Home' => URL::to('/'),
'Artikel' => ''
);
$this->layout->title = "Egoji";
$artikels = Posting::orderBy("created_at","desc")->where("postings.tipe","=","artikel")->paginate(5);
$this->layout->content = View::make("frontend.artikel.index",array("artikels"=>$artikels, 'breadcrumb'=>$breadcrumb))->render();
}
in the view frontend.artikel.index i am trying to takeout first artikel using $artikels->shift(). i get the first artikel, but it doesnt remove from the collection, it still there when i loop the rest.
As per here I've got the following controller:
class User extends CI_Controller {
public function Login()
{
//$data->RedirectUrl = $this->input->get_post('ReturnTo');
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('User_Login', $data);
}
//More...
}
and in my User_Login.php view file I do this:
<?php print_r($data);?>
which results in:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/User_Login.php
Line Number: 1
Do I need to load any specific modules/helpers to get the $data variable populated? If I print_r($this), I can see a lot of stuff but none of my data except in caches
Edit: To clarify, I know that calling the variable the same in the controller and view won't "share" it - it's out of scope but in the example I linked, it seems to imply a $data variable is created in the scope of the view. I simply happened to use the same name in the controller
Ah, the $data array's keys are converted into variables: try var_dump($title); for example.
EDIT: this is done using extract.
you should do it like :
echo $title ;
echo $heading;
echo $message;
Or you can use it like array.
In Controller:
...
$this->load->view('User_Login', array('data' => $data));
...
In View:
<?php print_r($data);?>
will show you the Array ( [title] => My Title [heading] => My Heading [message] => My Message )
you can pass a variable in the url to
function regresion($value) {
$data['value'] = $value;
$this -> load -> view('cms/template', $data);
}
In the view
<?php print_r($value);?>
You can't print the variable $data as it is an associative array....you may print every element of the associative array.....consider the following example.
Don't do as follows:
echo $data;
Do as follows:
echo $title;
echo $heading;
echo $message;
You can use this way also
$data['data]=array('title'=>'value');
$this->load->view('view.php',$data);
while sending data from controller to view we pass it in array and keys of that arrays are made into variables by code codeigniter and become accessible in view file.
In your code below all keys will become variables in User_Login.php
class User extends CI_Controller {
public function Login()
{
$data = array(
'title' => 'My Title', //In your view it will be $title
'heading' => 'My Heading', //$heading
'message' => 'My Message' //$message
);
$this->load->view('User_Login', $data);
}
}
and in your User_Login.php view you can access them like this:
echo $title;
echo $heading;
echo $message;