0

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.

Giu
  • 295
  • 1
  • 5
  • 22
  • you want to send page name from your array ? – Ashish Jan 18 '19 at 11:39
  • check below link https://stackoverflow.com/questions/20110757/laravel-pass-more-than-one-variable-to-view – kalaivanan Jan 18 '19 at 14:44
  • @Ashish Mmmh, I don't know where can I put my variables that contains information useful for every page/view. So, I would like to know wich is the best practise to do that. For example, modify the .env as suggested Sanjit Bhardwaj or modify the BaseController as suggested Prashant Deshmukh.....? – Giu Jan 18 '19 at 14:46
  • @kalaivanan ok, I know how to pass more than one variable to view. But where can I put this variables? In which file is better? – Giu Jan 18 '19 at 15:10

3 Answers3

1

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

Sanjit Bhardwaj
  • 893
  • 7
  • 13
  • But can I add or change variables in the .env? I thought the .env file was better not to change it. – Giu Jan 18 '19 at 14:24
1

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 ]);
}
Prashant Deshmukh.....
  • 2,244
  • 1
  • 9
  • 11
1

You can share variables for all views with View::share in AppServiceProvider

I had answered in another question. For details visit this: link

FGDeveloper
  • 1,030
  • 9
  • 23
  • Ok, thank you so much. Prashant Deshmukh..... suggested to put that code in the BaseController. Is there a file better then the other? Or if I write it in the file AppServiceProvider or BaseController is the same thing? – Giu Jan 18 '19 at 15:13
  • If you choose ``BaseController`` way, Your all controllers have to extends from ``BaseController`` class not Laravel's ``Controller``class. So, when you want create controller via ``artisan``, You have to change extends class in your generated Controller everytime It becomes tiring for me. :) – FGDeveloper Jan 18 '19 at 15:19
  • but if I use Controller.php? I don't need to extends every controllers created and I supposed that I have the same result. Is it right? Sorry, but I only try to understand which is the best way, and thank you so much for your patience. :) – Giu Jan 18 '19 at 15:26
  • 1
    Two ways may be correct way your needs too. It's depend your needs :) Example, if you want sidebar menu items in external sidebar.blade.php for every page, my way will be very very easy and manageable. if you don't need variables in every blade files for use, you may not want to use my way. An extra controller my be best way. – FGDeveloper Jan 18 '19 at 15:34
  • And if you think my answer is helpful, you can give agree my answer? – FGDeveloper Jan 18 '19 at 15:37