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.
Related
I´m a beginner starting with Laravel. I´m trying to show a question made on this website.
Here is the Controller page:
public function show($id)
{
//Use the model to get 1 record from the database
$question = $Question::findOrFail($id);
//show the view and pass the record to the view
return view('questions.show')->with('question', $question);
}
I have included at the top of the file:
use App\Question;
Here is my blade page:
#section('content')
<div class="container">
<h1> {{ $question->title }} </h1>
<p class="lead">
{{ $question->description }}
</p>
<hr />
</div>
#endsection
In the model I have not defined anything since I don´t need to specify any special rule. And finally here is the Route:
Route::resource('questions', 'QuestionController');
I got the error "ErrorException Undefined Variable: Question" and supposedly the error is on:
$question = $Question::findOrFail($id);
I´m looking forward to your observations.
Kind Regards.
You just need to change the controller section
public function show($id)
{
//Use the model to get 1 record from the database
$question = Question::findOrFail($id); // here is the error
//show the view and pass the record to the view
return view('questions.show')->with('question', $question);
}
Explanation:- You are going to use a variable $Question that is not defined. This is the basic PHP error, not the laravel issue.
However, You are using the "App\Question" model class not a sperate variable.
Currently I am working on a project where views need to be rendered through a custom Blade directive. However I came across a few (limitations?) errors I cannot solve (for a long time).
My custom Blade directive with tree different ways to output a view.
Blade::directive('lwField', function ($expression) {
// 1
return view('lw::module.field.field')->render();
// 2
return Blade::compileString('{!! view("lw::module.field.field")->render() !!}');
// 3
return Blade::compileString('#include("lw::module.field.field")');
});
The field view:
Get to the choppa!
The main view:
#extends('layout.default')
#section('main_content')
#lwField()
#endsection
The layout file:
// ... some cool html
#section ('main_content')
#show
// ... even more cool html
The following happens when I execute the three methods separately:
1 return view('lw::module.field.field')->render();
The fist time it executes it throws an error:
include(/somepath/storage/framework/views/b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php): failed to open stream: No such file or directory (View: /somepath/resources/views/page/default.blade.php)
This is true since there is only one view in my cached folder:
// 5b27802352643346357e49b847d934736c36cd07.php
// The main view with the field view
<?php $__env->startSection('main_content'); ?>
Get to the choppa!
<?php $__env->stopSection(); ?>
The second time I run this it magically works. It will generate 3 other files:
// b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php
// the main view with yielding layout view without field view
<?php $__env->startSection('main_content'); ?>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout.default', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
// e4cec91e7d4adb5dac5c63e5bfa85ba9a258f664.php
// the layout file
// ...
<?php endforeach; $__env->popLoop(); $loop = $__env->getFirstLoop(); ?>
// ...
// ffd60653d8490007c272c527abef3a5ede092a33.php
// layout view and main view
// ...
<?php $__env->startSection('main_content'); ?>
<?php echo $__env->yieldSection(); ?>
// ...
It looks like it generates two files that are the same but one without the field view.
2 return Blade::compileString('{!! view("lw::module.field.field")->render() !!}');
3 return Blade::compileString('#include("lw::module.field.field")');
These methods will return a blanko page and generate two files in cache without showing/logging any errors.
// 5b27802352643346357e49b847d934736c36cd07.php
// Field view
Get to the choppa!
// b30c24f5b8fd420ef1a08edb52e92174e2dfe911.php
// Main view and field view
<?php $__env->startSection('main_content'); ?>
<?php echo view("lw::module.field.field")->render(); ?>
<?php $__env->stopSection(); ?>
This method will not generate the layout file.
Does anyone have a clue on what is happening. Maybe this kind of behavior is not strange at all and this is how Blade should work.
I have been working on a project that required me to do the same thing, the problem is that making a new Blade directive is not a good practice when you want to render another view, It's because of the Cache, every Blade directive is being cached so if you want to render dynamic data it will cause you a lot of problems.
Since I have tried a lot of options and you want to render another view file and not just push html to the blade directive, I suggest you to make a new helper or even a service that you can pass there the parameters and it will return the render function that outputs clean HTML so this way you can be sure that your content will be always dynamic and won't be cached.
TL;DR
Example:
In your helpers functions file put:
function render_my_view() {
return view(''lw::module.field.field'')->render();
}
So then in yout blade file you can use:
{!! render_my_view() !!}
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
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');
How do I implement a sidebar in Zend Framework?
I know that I can use a placeholder or something similar in Zend_layout, but how do I automatically generate the code for the sidebar in my controllers without having to call a sidebar class within every controller?
My setup is as follows
Application
- modules
- blog
- other modules
I only want the sidebar for my blog module.
I have found this http://www.zfforums.com/zend-framework-components-13/model-view-controller-mvc-21/how-layout-sidebar-etc-2677.html but I do not understand the last part "just inject your layout, register it with the front controller ..."
You could just have a action and view in one of your controllers which renders the sidebar.
from the layout for the blog module you just call:
<? echo $this->action('action','controller','module',array('optionalparams'=>1); ?>
on the position where you want to have it. So one call to one action.
Rather than use the action stack and the action() view helper, you could render a "partial view script" that includes your sidebar elements.
# in your layout.phtml
<div id="sidebar">
<?php echo $this->render('blog/_sidebar.phtml'); /*relative to your view scripts directory*/ ?>
</div>
# in blog/_sidebar.phtml
<div id="blog_categories">
<?php foreach ($this->categories as $category): ?>
<?php echo $category->name; ?>
<?php endforeach; ?>
</div>
The render() view helper is used to render the content of another view script. It has the same scope as all your other view scripts, so if there are any variable assigned to the view, they will be available to your partial. So in the example above, the categories variable was set in the controller.
There is another view helper called the partial() view helper. This function is a little more expensive since it creates its own variable scope. In other words, none of your current view variables will be available. You will have a clean slate to work with, which means you must pass in any variables you need:
# in your layout.phtml
<div id="sidebar">
<?php echo $this->partial('blog/_sidebar.phtml', array('categories2'=>$this->categories)); ?>
</div>
# in blog/_sidebar.phtml
<div id="blog_categories">
<?php foreach ($this->categories2 as $category): ?>
<?php echo $category->name; ?>
<?php endforeach; ?>
</div>
I don't find myself using partial() very often since it is more expensive, and I rarely need to create a separate context.
As far as setting up the variables for use in the sidebar partial ($this->categories in this example), I have used a number of different methods depending on the particular problem. If it's specific to a controller action, I will write the code and assign it in the view script:
# controller
public function somethingAction()
{
$this->view->categories = $this->_getCategoriesForThisParticularAction();
// other controller code
}
If my code is more generic to all the actions of the controller, I will utilize the controller's preDispatch() function. If it's more generic to multiple controllers, I will put the code in the init() of my base controller (a controller the most of my controllers extend).
Sometimes I do not even put the code in my controller. If it's simple enough, I just stick the code in the partial. If it's a little more complex, I will move it to a view helper. This may break the MVC pattern, but I think it really depends on the particular case in order to determine the best placement.
If you are using Zend_Layout, just add the sidebar with the Action viewhelper as Rufinus said.
in your layout script:
<div id="sidebar">
<?php echo $this->action('action', 'controller', 'module', array('optionalparams'=>1)); ?>
</div>
<div id="content">
<?php echo $this->layout()->content; ?>
</div>
This should meet the requirements posted in your question.