How to use multiple layouts inside the same controller? - laravel

I wanted to ask how can I define a multiple layouts for the same controller in Laravel.
The scenario here is like the following:
I have a controller Home and i have two actions in this controller one called steps and the other called login.
I want the both of them load different layout.
The way that I used to make this is as follow:
protected $layout = "layouts.page";
public function index()
{
// Get to the page of the website making steps
$this->layout->content = View::make('steps');
}
Can I define multiple layouts? Maybe passing an array as follow:
protected $layout = array('first' => "layouts.page", 'second' => 'layouts.second');

Best solution is to create a method to generate your view, nesting your multiples layouts :
return View::make('layouts.master', array())
->nest('section_one', YOUR_SECOND_MASTER, array())
->nest...
and stop setting protected $layout with a layout.

I achieve in this way
$this->layout = View::make('layout.master');
$this->layout->content = View::make('step.demo')

Use View Composers or look at the section passing sub-views to views under http://laravel.com/docs/responses#views.
You can also specify multiple sections for the layout that is defined at http://laravel.com/docs/templates#blade-templating
EDIT:
If you want to define a master layout for different views from the same controller, then define the layout on the View it self. Take a look at the section Using A Blade Layout
The #extends is used to define the layout on the view itself.
Hope this helps for what you are looking for.

If you look at the BaseController, which your controller likely extends, you'll see the layout variable is ultimately used simply as th e result of any old View.
In other words, your $layout variable is just a View. You can create any $layout variable in your controller:
<?php
class MyController extends BaseController {
protected $layout;
protected $layout_alt;
// Here we're over-riding setupLayout() from
// the BaseController
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
if ( ! is_null($this->layout_alt))
{
$this->layout_alt = View::make($this->layout_alt);
}
}
}
Then in your view, you can return:
$this->layout_alt->content = View::make('steps');
Of course, the possibilities are endless as Abishek R Srikaanth pointed out. You can do fancy things with Blade as well :D

The way i do this is quite similar to #fideloper's answer.
protected $layout;
private $_layout = null;
public function __construct()
{
}
private function _setupLayout()
{
if ( ! is_null($this->_layout))
{
$this->layout = View::make($this->_layout);
}
}
public function home() {
$this->_layout = 'layouts.1col_public';
$this->_setUpLayout();
$this->layout->content = View::make('static/home');
}
public function about() {
$this->_layout = 'layouts.2col_public';
$this->_setUpLayout();
$this->layout->active_menu = 'about';
$this->layout->content = View::make('static/default');
}

This isn't common practise, and I haven't tested it yet, but it's worth a try.
In your controller's method:
$this->layout = View::make('layouts.master1");

Related

How to pass data to all views in Laravel 5.6?

I have two controllers. StudentController and TeacherController. I have a variable $chat which I want to pass in all the views of StudentController and TeacherController. The $chat will contain different data for both these controllers.
I searched and found ways, but I am getting empty data. I am doing it like this.
<?php
namespace App\Http\Controllers;
use View;
class StudentController extends Controller {
public function __construct()
{
$this->middleware('auth')->except(['home']);
$this->middleware('access')->except(['home']);
$chats = studentChat();
View::share('chats', $chats);
}
So, here I am printing and it is returning an empty array, but when I use the same in a function the array contains data. What is wrong here? Can anyone please help?
What I tried:
public function boot()
{
View::composer('*', function ($view) {
$chats = Cache::remember('chats', 60, function () {
if(Auth::user()->user_type() == config('constant.student'))
{
return studentChat();
}
else
{
return teacherChat();
}
});
$view->with('chats', $chats);
});
}
If you use View::share your share data to ALL your view, if you need to add data to few different views you may do this:
Create blade file(chat.blade.php for your case), and put your variables:
<? $chats = studentChat(); ?>
Include this file to the begining of your views where your need this 'global' varables:
//begin of your blade file
#include('chat')
//some code
{{ $chat->channel }}
Sharing Data With All Views
Occasionally, you may need to share a piece of data with all views that are rendered by your application. You may do so using the view facade's share method. Typically, you should place calls to share within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
$chats = studentChat();
View::share('chats', $chats);
}
public function register()
{
//
}
}
So, what I did was in the AppServiceProvider class, in the boot function I added this.
View::composer('*', function ($view) {
if(!\Auth::check())
{
return;
}
$userType = \Auth::user()->user_type ;
if($userType == config('constant.student'))
{
$chats = studentChat();
}
else if($userType == config('constant.teacher'))
{
$chats = teacherChat();
}
$view->with('chats', $chats);
});
You can pass data to the view Like.
return View::make('demo')->with('posts', $posts);
For more details visit article : Introduction to Routing in Laravel
write your query in boot method in appServiceProvider like,
View::composer('*', function ($view) {
$share_query = Cache::remember('share_query', 60,function () {
return App\User::all();
});
$view->with('share_query', $share_query);
});
Your final solution is ok, but not the cleanest possible.
Here is what i would do.
Define a class with a single function that contains your logic and return $chats, that way you will encapsulate your logic properly and keep your service provider boot method clean.
Then you have 2 options:
Inject your class in the boot() method of the service provider you use, then call its function and uses View::share. Should looks like :
public function boot(ChatResolver $chatResolver)
{
$chats = $chatResolver->getChats();
View::share(compact('chats));
}
If you only use $chats variable in a signe view or partial (like a part of layout), you can also inject the class you defined directly in the view.
Here is a link to Laravel doc regarding that.
In some cases it might be the easiest solution.

Laravel: master view or controller always called

How can I set a "default" view and/or controller so it will be always called no matter the route?
I have tried with:
(routes.php)
Route::get('/*', function(){
return View::make('master');
});
and:
Route::get('*', function(){
return View::make('master');
});
But gives me NotFoundHttpException;
Related question: Is there a better way to do this than setting it in the routes.php?
Thanks!
Laravel have something called Controller Layouts, then in your controller you can create a variable named $layout and set it to a master or default view:
class Controller extends BaseController
{
protected $layout = 'layout.master';
public function getIndex() {
$data[];
$this->layout->content = View::make('myview', $data);
}
Now in the layout.master view you can access the $content variable.

Passing the same data over different views in Laravel

I have for example this code in my HomeController:
public function index() {
$comments = Comment::get_recent();
$top = User::get_top_uploaders()->get();
$top_something = User::get_top_something_uploaders()->get();
$data = Post::orderBy('created_at', 'DESC')->paginate(6);
return View::make('index') ->with('data', $data)
->with('comments', $comments)
->with('top', $top)
->with('top_something', $top_something);
}
It works great, but I need to make another couple of view with the same data not only for index but also for other pages like comments(), post()...
How to make this in HomeController that I don't need to make it copy and paste those variables in every controller?
Pass your data using share method:
// for single controller:
class SomeController extends BaseController {
public function __construct()
{
$data = Post::orderBy('created_at', 'DESC')->paginate(6);
View::share('data', $data);
}
}
For all controllers you can put this code in BaseController's constructor
If the data is displayed using the same HTML each time you could put that piece of HTML into a partial and then use a View Composer.
Create a view and call it whatever you want and put in your HTML for the data.
In templates that need that partial include it #include('your.partial')
In either app/routes.php or even better app/composers.php (don't forget to autoload it)
View::Composer('your.partial', function($view)
{
$data = Post::orderBy('created_at', 'DESC')->paginate(6);
$view->with('data', $data);
});
Now whenever that partial is included in one of your templates it will have access to your data

Convenient Way to Load Multiple Databases in Code Igniter

I've added the appropriate configuration arrays to database.php and they work, however, I would like an easier way to access the different databases. Right now I have to do something like this in every controller method:
function index(){
$BILLING = $this->load->database('billing', TRUE);
$INVENTORY = $this->load->database('inventory', TRUE);
$data['billing'] = $BILLING->get('stuff');
$data['inventory'] = $INVENTORY->get('stuff');
}
I'd like to be able to put those first two lines in some sort of before filter or pre_controller hook.
You could simply load the database instances globally in your constructor, then they would be available to all controller methods...
example controller
class Example extends CI_Controller {
//declare them globally in your controller
private $billing_db;
private $inventory_db;
function __construct() {
parent::__construct();
//Load them in the constructor
$this->billing_db = $this->load->database('billing', TRUE);
$this->inventory_db = $this->load->database('inventory', TRUE);
}
function index() {
//Then use them in any controller like this
$data['billing'] = $this->inventory_db->get('stuff');
$data['inventory'] = $this->billing_db->get('stuff');
}
}
And if these same databases are used across multiple controllers, you might consider extending the base controller to include these global variables and load them in the constructor of your base controller in MY_Controller.php
example MY_Controller.php
class DB_Controller extends CI_Controller {
//declare them globally in your controller
private $billing_db;
private $inventory_db;
function __construct() {
parent::__construct();
//Load them in the constructor
$this->billing_db = $this->load->database('billing', TRUE);
$this->inventory_db = $this->load->database('inventory', TRUE);
}
}
Then you'd use it like this...
class Example extends DB_Controller {
function __construct() {
parent::__construct();
}
function index() {
//Then use them in any controller like this
$data['billing'] = $this->inventory_db->get('stuff');
$data['inventory'] = $this->billing_db->get('stuff');
}
}

Magento: use various methods in 1 model

I'm having a problem using methods in my module's model class.
I have a public function which triggers 2 protected methods. The problem is that only the first 1 returns a value.
Here is my class:
<?php
class Osdave_Points_Model_Mysql4_Points_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
const POINTS_CONFIRMED = 2;
const POINTS_REDEEMED = 4;
protected $_customer;
public function _construct()
{
parent::_construct();
$this->_init('points/points');
$this->_customer = Mage::getSingleton('customer/session')->getCustomer();
}
public function getCustomerAvailablePoints()
{
$confirmed = $this->_getCustomerConfirmedPoints();
$redeemed = $this->_getCustomerRedeeemedPoints();
$balance = ($confirmed - $redeemed);
return $balance;
}
protected function _getCustomerConfirmedPoints()
{
$availablePoints = $this->addFieldToFilter('customer_id', $this->_customer->getId())
->addFieldToFilter('points_status', self::POINTS_CONFIRMED)
->addFieldToSelect('points_pending')
->addExpressionFieldToSelect('available_points', 'SUM({{points_pending}})', 'points_pending');
return $availablePoints->getFirstItem()->getAvailablePoints();
}
protected function _getCustomerRedeeemedPoints()
{
$redeemedPoints = $this->addFieldToFilter('customer_id', $this->_customer->getId())
->addFieldToFilter('points_status', self::POINTS_REDEEMED)
->addFieldToSelect('points_pending')
->addExpressionFieldToSelect('redeemed_points', 'SUM({{points_pending}})', 'points_pending');
return $redeemedPoints->getFirstItem()->getRedeemedPoints();
}
}
Now, if, in _getCustomerRedeeemedPoints(), I replace $this by Mage::getResourceModel('points/points_collection') it works fine. But as I already am insdide the class, I don't understand why I have to instance it through Mage: as far as I understand, $this is only available once.
So, am I doing something wrong?
thanks in advance.
I'm guessing this has to do with adding filters to the $this object for different purposes. Try adding this to the top of your methods:
$this->getSelect()->reset();
If that doesn't work, try echoing your queries before your getFirstItem calls and see if they behave as expected:
Mage::log($this->getSelect()."");
Hope that helps!
Thanks,
Joe

Resources