Views

https://laracasts.com/series/laravel-from-scratch-2017/episodes/5

Table des matières

Views should be stored under /resources/views and, if we wish to use the Blade framework(more info), the file should be named view_name.blade.php.

If Blade isn’t needed, just name the file view_name.php. Blade is therefore not mandatory.

Be careful: the extension should be .php even if the file only contains HTML.

1. Calling a view

From a router

Route::get('article', function()
{
   return view('article');
});

Then just create /resources/views/article.blade.php with f.i.

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Our blog</title>
</head>
<body>
  Show article content
</body>
</html>

2. Passing parameters

2.1. Simple

Route::get('article/{n}', function($n) {
    return view('article')->with('number', $n);
})->where('n', '[0-9]+');
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Our blog</title>
</head>
<body>
  <p>Show article content #<?php echo $number; ?></p>
</body>
</html>

2.1.1. Shorthand

  return view('article')->with('number', $n);

can also be written

  return view('article')->withNumber($n);

Just concatenate the with keyword with the name of the variable.

  withNumber(...), withFirstName(...), withBirthDay(..), ...

3. Passing variables

Route::get('welcome', function () {

    $name = 'Avonture';
    $firstname = 'Christophe';
    $language = 'FR';

   return view('welcome', compact('name', 'firstname', 'language'));
});

compact() will take every variables and generate a key=value structure so

   return view('welcome', compact('name', 'firstname', 'language'));

is exactly the same of

  $name = 'Avonture';
  $firstname = 'Christophe';
  $language = 'FR';
  return view('welcome', [
    'name' => 'Avonture',
    'firstname' => 'Christophe',
    'language' => 'FR'
  ]);
});

In the view, we can use variables like this:

<div class="title m-b-md">
  Hello <?php echo $name . ', ' . $firstname;?>
</div>

Another syntax is

  $name = 'Avonture';
  return view('welcome')->with('name', $name);
});

4. Using querystring parameters

Imagine two URLs: http://site/notes and http://site/notes/5. The first URL will return all notes and the second, only the fifth one. So, in this case, the number five is well a parameter.

The router can looks like this:

// Get all notes
Route::get('/notes', function () {
   $notes = DB::table('notes')->get(); // getting notes for a table
   return view('notes.index', compact('notes'));
});

// Show the specified one
Route::get('/notes/{id}', function ($id) {
   $notes = DB::table('notes')->find($id); // getting only one note
   return view('notes.show', compact('notes'));
});

To make this example working, we’ll need two views, in /resources/views/notes/, one called index.blade.php and the second show.blade.php.

5. Tips

5.1. Return JSON

Laravel will automatically return JSON when the return statement is for a variable so

Route::get('getinfos', function () {

  $name = 'Avonture';
  $firstname = 'Christophe';
  $language = 'FR';

  $tasks = [];
  $tasks['1'] = 'My first task';
  $tasks['2'] = 'My second task';
  $tasks['3'] = 'My third task';
  $tasks['4'] = 'My last task';

  $server = $_SERVER;

  return compact('name', 'firstname', 'language', 'tasks', 'server');
});

will display

{
  "name": "Avonture",
  "firstname": "Christophe",
  "language": "FR",
  "tasks": {
    "1": "My first task",
    "2": "My second task",
    "3": "My third task",
    "4": "My last task"
  },
  "server": {
     "DOCUMENT_ROOT": "C:\\Christophe\\Repository\\app_test\\public",
     ...
  }
}

In that case, for just returning JSON, no view is needed and the conversion to JSON is done automatically.