I am trying to set flash data in Controller 1 and redirect to Controller 2, where I store the data in a $page_data variable to get it displayed in View 2. The following are my codes:
Controller 1:
$this->session->set_flashdata('message','my custom message');
redirect('controller2','refresh');
Controller 2:
$page_data['loginmessage'] = $this->session->flashdata('message');
$this->load->view('view2',$page_data);
View 2:
<p> <?php echo $loginmessage ?> </p>
CodeIgniter is able to load View 2, but does not display the login message. To put in another way, I am not getting the flash data message in Controller 2.
No need to use controller 2 for showing flashdata. Use this
Controller1
$this->session->set_flashdata('message','my custom message');
redirect('controller2/any_method');
Now you may use message in view2.php (which you are loading through controller 2) file as
<p> <?php echo $this->session->flashdata('message'); ?> </p>
You need to change the small piece of code your code to redirect it correctly.
Controller 1:
function first()
{
$this->session->set_flashdata('message','my custom message');
redirect("/controller2/second", "refresh");
}
Controller 2:
function second()
{
$page_data['loginmessage'] = $this->session->flashdata('message');
$this->load->view('view2',$page_data);
}
Also, if you refresh the page then flash data will disappear because it will appear only once.
View code is same
<p> <?php echo $loginmessage ?> </p>
Related
I want to show same content for different URL. For example:
website.com/controllername/country/state1/dist1/
website.com/controllername/country/state2/dist2/
I want these two URL to show same content.Even though the some content will vary based on URL (like name of state and district). I don't want to create separate content/page for every URL. I want to know if its possible to do so in Codeigniter? and is it possible to show a URL like the above in Codeigniter?
Yes it is possible with Codeigniter routing,
In your application/config/routes.php paste the below code and change the controller and function name you used,
$route['([a-zA-Z0-9---_%])+/([a-zA-Z0-9---_%])+/([a-zA-Z0-9---_%])'] = 'your_controller_name/your_function_name/$1/$1/$1';
Unlike other frameworks CodeIgniter does not have a global template system. Each Controller controls it's own output independent of the system and views are FIFO unless otherwise specified.
For instance if we have a global header:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >
<html>
<head>
<title><?=$title?></title>
<!-- Javascript -->
<?=$javascript ?>
<!-- Stylesheets -->
<?=$css ?>
</head>
<body>
<div id="header">
<!-- Logos, menus, etc... -->
</div>
<div id="content">
and a global footer:
</div>
<div id="footer">
<!-- Copyright, sitemap, links, etc... -->
</div>
</body>
</html>
then our controller would have to look like
class Welcome extends Controller {
function index() {
$data['title'] = 'My title';
// Javascript, CSS, etc...
$this->load->view('header', $data);
$data = array();
// Content view data
$this->load->view('my_content_view', $data);
$data = array();
// Copyright, sitemap, links, etc...
$this->load->view('footer', $data);
}
}
Yes you can set same page with different url setting on routing.
Refer below link for more info:
https://www.codeigniter.com/userguide3/general/routing.html
$route['controllername/country/state1/dist1/'] = 'catalog/product_lookup';
$route['controllername/country/state1/dist2/'] = 'catalog/product_lookup';
Improving the krishnaraj answer...
you need
website.com/controllername/method1/st/dist/
website.com/controllername/method2/st/dist/
//i´ve changed some variables for better understanding, and the "country" would
//be the main change
so at your controller you just
public function method1($st1,$st2){
$this->load->view('header');
$this->load->model('data_processing');
$data['data'] = $this->data_processing->any_model_function($st1,$st2);
$this->load->view('page_you_want',$data);
//here you choose the same view for both methods
$this->load->view('footer');
}
public function method2($st1,$st2){
$this->load->view('header');
$this->load->model('data_processing');
$data['data'] = $this->data_processing->any_model_function($st1,$st2);
$this->load->view('page_you_want',$data);
//here you choose the same view
$this->load->view('footer');
}
In the case of route configuration explained by Kavin Smk, noticed just a mistake, instead of
'your_controller_name/your_function_name/$1/$1/$1';
it shoud be
'your_controller_name/your_function_name/$1/$2/$3';
Well, there is always a lot of ways to do the same, choose yours...
Good luck!
I have a page system in Laravel - where I pass data from controller to view.
$this->data['title'] = $row->title;
$this->data['breadcrumb'] = $row->bc;
Now I passed it as follows:
return View::make('Themes.Page', $this->data);
In the view file, I access the data as follows:
{{$breadcrumb}}
What I am trying to do now is to pass this data in nested views:
$this->layout->nest('content',$page, $this->data);
(Content is the {{content}} in the view which will be replaced with $page contents. I want to pass the $this->data just as before but now I get an error:
Variable breadcrumb not defined.
Note: Laravel Version 4.2 $this->layout is set in constructor to a
template file (Themes.Page)
Actually you don't need to pass any separate data to your partial page(breadcrumb)
controller page
$this->data['title'] = $row->title;
$this->data['breadcrumb'] = $row->bc;
return View::make('idea.show',array("data"=>$this->data));
main view page
<div>
<h1>here you can print data passed from controller {{$data['title']}}</h1>
#include('partials.breadcrumb')
</div>
your partial file
<div>
<h1>here also you can print data passed from controller {{$data['title']}}</h1>
<ul>
<li>....<li>
<li>....<li>
</ul>
</div>
for more information on this you can check following links http://laravel-recipes.com/recipes/90/including-a-blade-template-within-another-template or watch this video https://laracasts.com/series/laravel-5-fundamentals/episodes/13
You should pass data as follows
return View::make('Themes.Page')->with(array(
'data'=>$this->data));
or (since you're passing only 1 variable)
return View::make('Themes.Page')->with('data', $this->data);
and further you can pass it on to nested views as by referencing $data
$dataForNestedView = ['breadcrumb' => $row->bc];
return View::make('Themes.Page', $this->data)->nest('content', 'page.content', $dataForNestedView);
In the Themes.Page view render nested view:
<div>
{{ $content }} <!-- There will be nested view -->
</div>
And in the nested page.content view you can call:
<div>
{{ $breadcrumb }}
</div>
*div tag is only for better understanding.
Okay, after intense search, I figured out that there was a bug in the Laravel version 4.2 I was using.
Laravel 5 works.
For laravel 4.2, a better option would be to pass array of data objects using View::share('data',$objectarray) while passing the data from controller.
Thanks everyone for help
My URL is:
http://localhost/CodeIgniterCms/admin/dashboard/otherpages/123
where,
Codeigniter Cms
- admin
-- dashboard (the controller)
--- otherpages (Method)
Controller code
public function otherpages($somedata) {
$this->render('admin/second_view',$somedata);
}
Code in second_view.php
<pre>
<div class="container">
<?php echo $somedata;?>
</div>
</pre>
But it is throwing error
Unable to load the requested file: admin/123.php
Try this within your otherpages function:
$data['somedata'] = $somedata; <br/>
$this->load->view('admin/second_view',$data);
In my main.php, I created a link and it's supposed to display all announcements that the current user posted but instead, I'm getting ALL the announcements from every user.
How do I change this?
This is my link code:
<a class="more" href="<?php echo Yii::app()->createUrl('announcement')?>" >
<?php switch_lang('View Announcements', '查看更多', FALSE)?>
</a>
And based on my code from the actionShow() from the controller, this is the code:
public function actionShow($id)
{
$post=$this->loadModel($id);
$comment=$this->newComment($post);
$attachments=Attachments::model()->findAllByAttributes(array(
'content_id' => $id,
));
$this->render('show',array(
'model'=>$post,
'comment'=>$comment,
'attachments'=>$attachments
));
}
If you are trying to work the show action of whatever controller it is a part of, do this -
<a class="more" href="<?php echo Yii::app()->createUrl('<controllername>/show',array('id'=>$id))?>" >
You can re-route the action to any name like the 'announcement' part you gave in the question in the urlManager of main.php subsequently.
I'm new to CodeIgniter and try to understand how to create forms. I searched both on the Net and stackopverflow but did not reach anything.
What i want is to create forms with helpers.
In order to do that in my controller create a function, named formElements() and the code is
public function formElements()
{
$this->load->helper(array('form'));
}
and in kayit.php
i try to create some html elements
<?=form_open('kayit/formElements')?>
<?=form_fieldset('Login Form')?>
<div class="textfield">
<?=form_label('username', 'user_name')?>
<?=form_input('user_name')?>
</div>
<div class="textfield">
<?=form_label('password', 'user_pass')?>
<?=form_password('user_pass')?>
</div>
<div class="buttons">
<?=form_submit('login', 'Login')?>
</div>
<?=form_fieldset_close()?>
<?=form_close();?>
However i take the error : Fatal error: Call to undefined function form_open() in C:\xampp\htdocs\pasaj\application\views\kayit.php on line 220
why?
You can also include the form helper in ./application/config/autoload.php file
$autoload['helper'] = array('form');
You can include the form helper in your Controller's constructor function, like:
$this->load->helper('form');
And it should work fine in view.
Ref: Form Helper
Hope it helps