Share 2 data to all view in laravel 4.2 - php

I want to share this data to all views with laravel 4.2
But, can I use array to pass this 2 data with View::share()?
class Sidebar extends BaseController {
public function __construct() {
$package_sidebar = TravelPackage::orderBy('idTravelPackage','DESC')->take(4)->get();
$artikel_sidebar = Artikel::orderBy('date','DESC')->take(4)->get();
View::share()
}
}

Put this in your routes.php or somewhere else more appropriate:
<?php
View::share('package_sidebar', TravelPackage::orderBy('idTravelPackage','DESC')->take(4)->get());
View::share('artikel_sidebar', Artikel::orderBy('date','DESC')->take(4)->get());
Then you'll be able to reference $package_sidebar and $artikel_sidebar in any view.

Related

Include different variables in some views passing through controller

I'm new of Laravel and I have started my first project (with Laravel 5.7).
I have some variables that I would like to use in every single view.
In general I create, for example, a config.php file where I put my variables and use them in every pages (obviusly including config.php in all pages).
But, with Laravel, where can I put this variables? And how can I do to use them in all views?
This is my web.php:
Route::get('/task','TaskController#index');
Route::get('/task/insert','TaskController#setInsertTask');
Route::get('/task/list','TaskController#getTaskList');
And in the TaskController:
class TaskController extends Controller
{
public function index(){
return view('task.index');
}
public function setInsertTask(){
return view('task.insert');
}
public function getTaskList(){
return view('task.list');
}
}
Now I have tried to put the variables in the TaskController like this:
class TicketController extends Controller
{
private $titlePage1 = "Task manager";
private $titlePage2 = "Task manager insert";
public function index(){
return view('task.index',[
'titlePage' => $this->titlePage1
]);
}
public function setInsertTask(){
return view('task.insert',[
'titlePage' => $this->titlePage2
]);
}
public function getTaskList(){
return view('task.list',[
'titlePage' => $this->titlePage1
]);
}
}
And in the view I have insert something like this:
#extends('layout.layout')
#section('content')
<h1>{{ $titlePage }}</h1>
#endsection
But I don't think that is the best solution and I don't like it.
In this project I would like to use this variable beacuse:
1. I would like to managed three different software related each other (ex. Login for users, login for admin, login for technicians) thet they have the same database and the single area is small. So, for each area I'll liked to print a different title.
2. In the pages there are some static word, so I will create an array with all words in such a way to concenter all static words.
3. Like the title page, I would like the same things with a menu. Different menus for different areas managed in a single file in php (not in the html).
4. The same variables I will like to use them in other controllers.
I have searched a lot but I can't find which is the best practise to include a general variable in some views.
Can anyone help me? Thanks a lot.
You can share variables for all views with View::share in AppServiceProvider
I had answered in another question. For details visit this: link
Yes, you can use the variables defined in the .env at route
for example in .env
name=test
You can get it as env('name')
read here
You can do it using BaseController
class BaseController extends Controller
{
public function __construct()
{
$titlePage1 = "Task manager";
$titlePage2 = "Task manager insert";
View::share(['titlePage1' => $titlePage1, 'titlePage2' => $titlePage2 ]);
}
}
You can access it in any view {{$titlePage1}} and {{$titlePage2}}
You can also perform same thing with AppServiceProvider
In boot() of AppServiceProvider, add following code.
public function boot() {
$titlePage1 = "Task manager";
$titlePage2 = "Task manager insert";
View::share(['titlePage1' => $titlePage1, 'titlePage2' => $titlePage2 ]);
}

Yii1: Controller::beforeRender in Yii2

I'm migrating an old app developed in Yii1 to Yii2.
I used to have a array in the controller that was storing all the variables that I would need to send to the frontend as a JavaScript:
public $jsVars;
public function toJSObject($params){
$this->jsVars = array_merge($this->jsVars, $params);
}
private function printJSVarsObject(){
//convert my php array into a js json object
}
When I needed a variable to be exposed in Javascript, I would just use $this->toJSObject, in the View or in the Controller.
Then, in the controller I also used to have:
public function beforeRender($view){
$this->printJSVarsObject();
}
In Yii2, I had to configure the View component with a custom View and then attach an event:
namespace app\classes;
use yii\base\Event;
use yii\helpers\Json;
Event::on(\yii\web\View::className(), \yii\web\View::EVENT_END_BODY, function($event) {
$event->sender->registerJSVars();
});
class View extends \yii\web\View {
public $jsVars = [];
public function addJsParam($param){
$this->jsVars = array_merge($this->jsVars, $param);
}
public function registerJSVars() {
$this->registerJs(
"var AppOptions= " . Json::htmlEncode($this->jsVars) . ";",
View::POS_END,
'acn_options'
);
}
}
But, having the event outside the class seems weird to me. Also, while I'm in the controller, I won't be able to use my former approach using this method.
Obviously, I'm missing something, or my approach is just incorrect.
How do you guys do that?
If you're trying to access properties of the controller from a view (see above comments!), you can use;
$this->context
to return an instance of the currently used controller from within the view file. So to access your beforeRender() method you would just use
$this->context->beforeRender()

Saving object in a class variable and using it in another function - php, laravel

So here's the code
use App\Video;
class HomeController extends Controller
{
protected $video;
public function index()
{
// $video_to_watch is fetched from db and I want to save it and use it in
// another function in this controller
$this -> video = $video_to_watch;
return view('home', compact('video_to_watch'));
}
public function feedback(Request $request)
{
dd($this -> video);
}
}
feedback returns null for some reason.
when I put the
dd($this -> video);
in index() it works fine, not null.
I have tried what's suggested here: Laravel doesn't remember class variables
but it didn't help.
I'm sure it's something stupid I'm overlooking. But can't seem to figure out what, any help much appreciated.
You can't keep your $video value between 2 different requests. You have to fetch your video data in each request.
use App\Video;
class HomeController extends Controller
{
public function index() {
$myVideo = $this->getMyVideo();
return view('home', $myVideo);
}
public function feedback(Request $request) {
dd($this->getMyVideo);
}
private function getMyVideo() {
// fetch $video_to_watch from db
return $video_to_watch ;
}
}
First of all don't fetch data inside a Controller. It's only 'a glue' between model and view. Repeat. No fetching inside a controller.
Use domain services and dependency injection to get business data and if you want to share this data create shared service (single instance).
-
Putting a data object into a controller property class makes a temporary dependency between method calls. Avoid it. Use services instead.

Laravel: Changing layout in controller method

I have a controller which, within a method, I want to specify a different layout for my view. I would have thought the below would do the trick, however, I am getting:
Attempt to assign property of non-object
on the line where I am trying to reset the layout.
class My_controller extends Base_Controller {
public $layout = "cms::layouts.default";
............
public function get_list($status = "open")
{
$this->layout = 'cms::layouts.nowrap';
$this->layout->strContent = View::make('cms::partials.orderdetails')
->with('xxxx', \CMS\XXX::method($xxx));
}
.................
}
Any ideas? I'm using Laravel 3 for this one
I think that you have to do that :
$this->layout = View::make('cms::layouts.nowrap');
$this->layout->strContent = ...;
It's the L4 solution but, after a small check, it seems to be the same pattern in L3.

Nesting controllers and views in Laravel

I have a controller in my Laravel project called ImageController. This is a pretty basic CRUD controller.
When I access /images/{id} through my ImageController#show action, I want to also display comments. However, I don't want to put the comment logic in my ImageController. For this logic, I have created an ImageCommentController.
I'm not really sure how to go about this, but I'm trying to do something of this sort:
class ImageController extends BaseController {
// methods ...
public function show($id)
{
$images = // get images ...
$this->layout->view = // images.show and imagescomment.index (using ImageCommentsController#index logic)
}
}
I'm sorry if this is vaguely phrased, let me know if it is and I'll try to make it more understandable.
Maybe a better solutions than using a Controller for displaying the comments is to use a class with a method renderComments() that basically does something like:
class Comments {
public static renderComments($commentType = 'images')
{
$comments = Comments::where('comment_type', '=', $commentType)->get();
return View::make('comments', $comments)->render();
}
}
Then for example inside your image view:
...
{{ Comments::renderComments() }}
...

Categories