I am using CodeIgniter and I have a controller called contact which passes data to it's view which I have loaded in via the header so that it's on every page, however the data I pass to this view doesn't appear. It only appears if I go straight to the view via the url and I can only presume that this is caused because it is pulled in via another view which has a different controller? Is that right, if so how do I fix it?
For example:
<body>
<div id="header">
<h1>Hello there!</h1>
<?php echo $this->load->view('contact'); ?>
</div>
Are you parsing any data to the 'contact' view? If so, how?
CodeIgniter Userguide - Loading a View
function contact()
{
$data['someinfo'] = "Some Info";
$this->load->view('contact', $data);
}
the problem is calling the 'contact' view from another view doesn't mean the 'contact' controller is being called... that is why you are unable to access the data passed from the 'contact' controller!
To call controller from views you will need https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
Related
This code works well in the "images" view under images_model:
<?php if($images_model):?>
<?php foreach($images_model as $images):?>
<div class="col-sm-4">
<div class="well">
<img src="<?php echo base_url()."uploads/".$images->name;?>" alt="" class="img-thumbnail">
The code in images_model is the following:
function save_image($data){
$this->db->insert('images', $data);
}
When I try to use the same code in the "wall" view (main_controller) I get the severity notice error saying "images_model" is undefined; even though I load the images_model in the main_controller or auto-load the both models:
$autoload['model'] = array('main_model', 'images_model');
I originally questioned the "foreach" code in the "images" view, but I thought if it works in one view shouldn't it also work in another if I load the related model? It just doesn't seem to be reading the images_model.
I'm just getting to know codeigniter a bit and would appreciate any feedback.
Are you passing the $images_model variable to your view from your main_controller ?
// Set your variable
$data['images_model'] = $images_model;
// Pass variable to view
$this->load->view('main', $data);
You typically shouldn't have access to a model directly in the view (following MVC principals). Your controller should get what it wants from the model, then pass it to the view.
Hi I am very new to Laravel and MVC frameworks in general and am looking to create a list of links (in a view within a template) that links to some content. I am using this to display a list of nine people and to display their profile description when the link is clicked on. I have created a model of what the page looks like at http://i.imgur.com/8XhI2Ba.png. The portion that I am concerned with is in blue. Is there a way to route these links to something like /about/link2 or /about?link2 while maintaining the same exact page structure but modifying the ‘link content’ section (on the right of the link menu) to show the specific link's content? I would greatly appreciate it if someone could point me in the right direction, as I have literally no clue where to go with this!
There are a couple ways you can go about doing this.
Templates
Create your route.
Im assuming a lot about your app here but hopefully you get the picture. If you need help with anything in particular, be sure to update your question with the code youve tried so it will be easier to help you.
Route::get('about/{page}', function($page)
{
$profile = Profile::where('name', $page)->first();
return View::make('about')->with('profile', $profile);
});
Modify Template.blade.php
Put this line where you wish for About.blade.php to appear.
#yield('content')
Create your view which will extend your template
#extends('Template')
#section('content')
<h2>User Profile</h2>
<ul>
<li>Name: {{ $profile->name }}</li>
<li>Last Updated: {{ $profile->updated_at }}</li>
</ul>
#stop
AJAX
This solution will utilize AJAX to grab the data from the server and output it on the page.
Route for initial page view
Route::get('about', function($page)
{
$profiles = Profile::all();
return View::make('about')->with('profiles', $profiles);
});
Feel free to follow the same templating structure as before but this time we need to add some javascript into the template to handle the AJAX. Will also need to id everything which needs to be dynamically set so we can easily set it with jquery.
#extends('Template')
#section('content')
<h2>Links</h2>
#foreach($profiles as $profile)
{{ $profile->name }}
#endforeach
<h2>User Profile</h2>
<ul>
<li>Name: <span id="profile_name">{{ $profile->name }}</span></li>
<li>Last Updated: <span id="profile_updated_at">{{ $profile->updated_at }}</span></li>
</ul>
<script>
function setProfile(a)
{
$.ajax({
method: 'get',
url: 'getProfile',
dataType: 'json',
data: {
profile: $(a).data('id')
},
success: function(profile) {
$('#profile_name').html(profile.name);
$('#profile_updated_at').html(profile.updated_at);
},
error: function() {
alert('Error loading data.');
}
});
}
</script>
#stop
Route to handle the AJAX request
Route::get('getProfile', function()
{
$profile_id = Input::get('profile');
$profile = Profile::find($profile_id);
return $profile->toJson();
});
Now, the page should not have to reload and only the profile information should be updated.
Making some assumptions here as no code posted and assuming you're using the latest version of Laravel, Laravel 5.
Lets say you have a table in your database named users and you have a Model named Users (Laravel 5 comes with the Users model as default, see app/Users.php). The users will be the base of our data for the links.
Firstly, you want to register a route so you can access the page to view some information. You can do this in the routes file. The routes file can be found here: app/Http/routes.php.
To register a route add the following code:
Route::get('users', ['uses' => 'UserController#index']);
Now what this route does is whenever we hit the URL http://your-app-name/public/users (URL might be different depending on how you have your app set up, i.e. you may not have to include public) in our web browser it will respond by running the index method on the UserController.
To respond to that route you can set up your UserController as so:
<?php namespace App\Http\Controllers;
class UserController extends Controller {
public function index()
{
}
}
Controllers should be stored in app/Http/Controllers/.
Now lets flesh out the index method:
public function index()
{
// grab our users
$users = App\Users::all();
// return a view with the users data
return view('users.index')->with('users');
}
This grabs the users from the database and loads up a view passing the users data.
Here's what your view could look like:
<!DOCTYPE html>
<html>
<head>
<title>Users Page</title>
</head>
<body>
#foreach($users as $user)
<a href="{{ URL::route('user', ['id' => $user->id]) }}">
{{ $user->username }}
</a>
#endforeach
</body>
</html>
The view code will loop through each user from the $users data we passed to the view and create a link to their user page which is different for each user based on their id (their unique identifier in the DB)
Due to the way I've named it, this would be found in app/views/users/index.blade.php - if you save files ending in blade.php you can use Laravel's templating language, blade.
Now you need to finally set up another route to respond to a user page, for example http://your-app-name/public/user/22.
Route::get('user/{id}', ['uses' => 'UserController#show']);
Then add the show method to UserController
public function show($id)
{
// this will dump out the user information
return \App\User::find($id);
}
Hope that helps a little! Wrote most of it off the top of my head so let me know if you get any errors via comment.
This question is very bare, and it is difficult to actually help your situation without you showing any code. Just to point you in the right direction though, here is what you would need.
A Model called People, this is how you will access your data.
A controller. In this controller you will do the following
Get the ID of the profile you want from the functions parameters
Find that persons information e.g. People::find($person_id);
return the profile view with that persons data e.g. return view('profile')->with('person', $person);
In your view you can then use that data on that page e.g. {{ $person->name }}
For the page that needs to display the links to the people you would have a method in your controller which..
Get all the people data e.g. People::all();
Return a view with that data return view('all-people')->with('people', $people);
You will then need a route to access an individual person. The route will need to pass the persons ID into a controller method e.g.
Route::get('get-person/{id}',
[ 'as' => 'get-person',
'uses' => 'PeopleController#getPerson' ]);
You can then use this route in your view to get the links to each person
#foreach($people as $person)
{{$person->name}}
#endforeach
This would produce the list of links you want.
Is there a way to disable layout for certain controller method?
Im using something like $this->layout = null ,yet it still render the layout
The view im rendering obviously have a layout associate with it, i just wonder is it possbile to disable the layout from within controller method, without need to modify the blade file itself
Here is the controller:
class PurchaserController extends \BaseController
{
public function index()
{
$this->layout = null;
return View::make('purchasers.index');
}
}
The view:
#extends('layouts.master')
#section('content')
Content
#stop
Im using Laravel 4
Just remove
#extends('layouts.master')
from your view. That will prevent the view from loading.
Also - if you are using the #extends - then you dont actually need $this->layout() in your controller at all
Edit:
" i just wonder is it possbile to disable the layout from within controller method, without need to modify the blade file itself"
The idea is you do it either entirely from the controller, or entirely from the blade file. Not both together.
lets say that i hv this view (main)
<body>
lorem epsim
<div table></div>
lorem epsim
</body>
in controller control1.php i do
$this->load->view('header');
$this->load->view('main',$data);
$this->load->view('footer');
Now i need to load content of div=table from another view (tbl.php),which is called from another controller
control2.php
function load_table(){
$data['x']=1;
$this->load->view('tbl.php',$data);
}
tbl.php view
<ul>$x</ul>
how can i do that ?
i tried to load controler 2 from controller 1 and assign the function load_table to variable and pass that to main view, but it didnt work cuz load->view is executed instead of saving output to variable..
Reason:
i need to do this is that tbl.php view is a complex table that i need to refresh and load via ajax calls, so i need it to be on different view alone
so can some one explain to me how can i work this out ?
You can't call one controller method from another, separate controller. You can, however, get the output of the table view and use that:
// main.php
<body>
lorem epsim
<div table><?php echo $table_content; ?></div>
lorem epsim
</body>
.
// control1.php
$table_data['x'] = 1;
$data['table_content'] = $this->load->view('tbl.php', $table_data, TRUE);
$this->load->view('header');
$this->load->view('main',$data);
$this->load->view('footer');
So, you get the data to pass to the tbl.php view and pass that to the load->view method - also passing TRUE as the third parameter - which will return the contents of that view as a string (instead of outputting that to the browser). Now, you have a $data variable to pass to the main view with the table html included and you can just echo that out in the main view.
How you get the $data['table_content'] data from the view is up to you. You can create another controller method inside control1.php, you can create a helper file that can load the view into a string and return that, etc.
Maybe you can create a view with the table code within and then you can do inside your div for ajax
<div id="for_ajax">
<?php $this->load->view('table'); ?>
</div>
I've similar needs but mine its like a comments wall for issues.
Inside a view use following in a php block
$CI = &get_instance();
$CI->load->view('view_name');
I'm trying to display an image but is dependent on a dropdown list in Yii. I can get the image from the database and display it, but how to do it dynamically depending on the choice from the dropdown?
Here is the reference: http://www.yiiframework.com/wiki/24/creating-a-dependent-dropdown#hh0 but, let me show you how to do it.
First all all, we need a div where the image will be displayed; I'll create one whose id will be 'img'. Then, the ajax request is specified inside the dropdownlist() as follows:
<?php echo $form->labelEx($model,'attribue'); ?>
<?php echo $form->dropDownList($model,'attribute',
array(/*The options in the DropDownList*/),
array(
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('YourController/actionWhichEchoesTheImage'),
'update'=>'#img',
)));
?>
<div id="img"> // <---- the result of the ajax call will be displayed here
</div>
In the 'url' attribute we specify the function which will be called when the ajax request triggers. In the 'update' attribute we specified the div where will be displayed the result of calling that function (the image).
Finally, we have to declare the action actionWhichEchoesTheImage(). Let's declare it in the current controller. It would look something like this:
public function actionWhichEchoesTheImage()
{
if(isset($_POST['ModelName']['attribute']))
/*Here goes your code to load the image*/
echo CHtml::image(//Check the reference to see how to set this function);
}
Check CHtml::image() here: http://www.yiiframework.com/doc/api/1.1/CHtml/#image-detail