Laravel basic MVC - setting values in a controller and pass it to a view

StackOverflow https://stackoverflow.com/questions/23667312

  •  22-07-2023
  •  | 
  •  

Вопрос

O.k. I just set up Laravel and I'm missing a basic concept.

Route

Route::get('controllertest', 'ControllerTest@getIndex');

Controller

public function getIndex()
{
    return View::make('test_controller');
}

View

<title>{{ $title }}</title>

I've tried everything and I can't get a variable to pass from the controller to the view.

Everything works but my title is still {{ $title }}

Yes, this is basic stuff, but how do you do it?

Это было полезно?

Решение

You can pass variables to your views in the following way:

public function getIndex() {
    return View::make('test_controller', [
        'title' => 'Title Value'
    ]);
}

An alternative is to use the following syntax:

public function getIndex() {
    return View::make('test_controller')->with('title', 'Title Value');
}

Check their documentation for more detail.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top