How to call data and view it on the footer? - laravel

This is the function that I am using in my controller
public function homeList()
{
//Get all the franchises
$franchises = Franchise::all();
//Load the view and pass the franchises
return View::make('frontend.layouts.footer')->with('franchises', $franchises);
}
and I keep getting this error. I don't know how to pass it or what to put on the routes.php file

You should be able to do #include('frontend.layouts.footer')->with('franchises', Franchise::all()).
Update:
To avoid the model in the view, you should use a view composer, as it was stated in a previous answer.
View::composer('frontend.layouts.footer', function($view)
{
$view->with('franchises', Franchise::all());
});
Now you have $franchises available in the view. This code you can place in routes.php or you can create a composers.php and autoload it.

Related

CodeIgniter Dry Navigation

My boss told me make dry navigation dont use repetitive code, for navigation i am trying to extend CI_Controler and with construct load header nav, body, footer files.
My question is when i create new controller and when i try to load different view files, how to achive that???
my extended controler
class MY_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->view('view_header');
$this->load->view('includes/nav_home');
$this->load->view('view_home');
$this->load->view('view_footer');
}
}
and later when i am creating new controler how to load diffrent view files
class Pages extends MY_Controller
{
public function __construct() {
$this->load->view('includes/nav_new_view');
$
}
}
You can create a template library yourself. For example :
function template($view_name,$view_data){
//below will return html string from view name
$data['content'] = $this->load->view($view_name,$view_data,true)
//load main template view and pass the html string to main template
$this->load->view('main_template',$data);
}
In main template, just echo $content
If I understand your question, you're trying to achieve a template situation. For this, the best way is to actually call your templates view files within a primary page view. What I mean is your controller function (not the constructor, an actual class function representing a page) should call a primary view such as
$this->load->view('page1', $this->data);
and within that file, you call
$this->load->view('nav', $this->data);
then your content and then
$this->load->view('footer', $this->data);
You would then repeat the process for page 2 where in your controller's page2 function, you would call
$this->load->view('page2', $this->data);
and your page2 view file is almost identical to page1 except you use your page 2 content in that area.
You could even use a single template view file and pass it a $content variable (which obviously changes per page) and call
$this->load->view('template', $this->data);

Serialize Laravel Query Builder

I would like to be able to construct a query using laravel, and serialize it into a url string.
This would allow me to create routes which would unserialize a query builder, run the query, and make a view which displays the database results.
For example, to implement a button which refreshes a list of posts made by kryo:
http://example.com/ajax/posts.php?name=kryo&order_by=created_at&order_type=desc
Posts.php would simply be a route which unserializes, validates, and runs the query in the url params, and provides the results to a view.
Perhaps this is not useful in general, but I would personally find it handy specifically for ajax requests. If anyone knows how to implement this as a laravel plugin of some nature, that would be fantastic.
I'll try to give you a basic idea:
In Laravel you have to create a route to make a request to a function/method, so at first you need to create a route which will be listening for the ajax request, for example:
Route::get('/ajax/posts', array('uses' => 'PostController#index', 'as' => 'showPosts'));
Now, create a link in the view which points to this route, to create a link you may try this:
$url = to_route('showPosts');
If you use something like this:
<a class='ajaxPost' href="{{ $url }}?name=kryo&order_by=created_at&order_type=desc">Get Posts</a>
It'll create a ink to that route. So, make sure you are able to pass that $url to your JavaScript or manually you can write the url using /ajax/posts?name=.... Once you done creating the link then you need to create your JavaScript handler for this link (maybe using click event) then handle the click event from your handler, make ajax request, if it's jQuery then it could be something like this:
$('.ajaxPost').on('clcik', function(e){
e.preventDefault();
var url = $(this).attar('href');
$.getJSON(url, function(response){
$.each(response, function(key, value){
// loop... you may use $(this) or value
});
});
});
In your PostController controller class, create the index method:
class PostController extends BaseController {
public function index()
{
$name = Input::get('name');
$order_by = Input::get('order_by');
$created_at = Input::get('created_at');
$order_type = Input::get('order_type');
$posts = Post::whereName($name)->orderBy($order_by, $order_type)->get();
if(Request::ajax()) {
return Response::json($posts);
}
else {
// return a view for non ajax
}
}
}
If you want to send a rendered view from the server side to your JavaScript handler as HTML then change the getJson to get and instead of return Response::json($posts); use
return View::make('viewname')->with('posts', $posts);
In this case make sure that, your view doesn't extends the master layout. This may not be what you need but it gives you the idea how you can implement it.

How to load different view in joomla controller?

I have simple line, but it doesn't work.
$this->getView($input->get('my_wiew', 'Sites', 'CMD'), 'HTML');
//some code
parent::display();
If i simple go to the url index.php?option=com_my_component&view=sites i get my view, but by default it doesn't want to load.
$view = $this->getView('view_name', 'html'); //get the view
$view->assignRef('data', $data_from_model); // assign data from the model
$view->display(); // display the view
Read more

CodeIgniter - Autoload for views?

is there any way to load a view (for header or footer) on every function (in a controller)? I have a couple of if/else statements there and it would be a pain to change it all when I'll need to.
Yes, you can load the view in the __construct function at the top of your controller. Take
a look at the PHP manual on Constructors
function __construct()
{
parent::__construct();
$this->load>-view('your_view');
}
If the header and footer are going to be constant and required components for the visual part of your site, but you may want to load a different content portion between your header and footer, then you can make a function that will take an argument.
private function doViews($argument)
{
$this->load->view('header');
$this->load->view($argument);
$this->load->view('footer');
return NULL;
}
You may want to have an array of available views inside the doViews function in order to do proper validation that the file exists. Then you simply call the function in each method in your controller like this:
$this->doViews('main_content');
You should try using a Template library like this: https://github.com/philsturgeon/codeigniter-template
Then all you need to is put this in the controller (can be in __construct or within a method)
$this->template->set_partial('header', 'layouts/header');
$this->template->set_partial('footer', 'layouts/footer');
$this->template->set_partial('sidebar', 'layouts/sidebar');
Then to send data like you do with a view:
$this->template->build('create', $this->data);
you could create your main_view ... as a master page that already has a structure:
main_view.php
$this->load>-view('header');
<?php //get content here
?>
$this->load>-view('footer');
If you want to change something in the header or footer (through a statement) you can add content:
function __construct()
{
parent::__construct();
$data['footer'] = ($a == 'foo') ? 'footer one' : 'footer two';
$data_to_main = $this->load->view('template/footer', $data, TRUE);
$data_to_main = 'others';
$this->load>-view('main_view', $data_to_main);
}

add iframe while calling controller to view in codeigniter

call controller to view i use this code..
i want to add "template/home/home" page in to iframe tag while calling controller to view.
how can i do?
class main_con extends Controller
{
function main_con()
{
parent::Controller();
// $this->freakauth_light->check('user');
$this->_container = $this->config->item('FAL_template_dir') . 'template/container';
$this->load->library('FAL_front', 'fal_front');
}
function index()
{
$data['redirect_page'] = 'template/home/home';
$this->load->vars($data);
$this->load->view($this->_container);
}
}
thank you in advance...
I'm not quite sure if I understand your problem but:
The iframe is in your view template right? So what you need to do is just print the content of your variable in the src attrib of the iframe.

Resources