Home  >  Article  >  Backend Development  >  Laravel 5.4 Getting Started Series: Routing and Views

Laravel 5.4 Getting Started Series: Routing and Views

巴扎黑
巴扎黑Original
2018-05-16 16:22:392495browse

Main knowledge points:

  • Basic process from routing to view

  • Data transfer

Let’s take a look at how the last page of the first lecture comes out. Let’s take a look at routing first:

// /routes/web.php
Route::get('/', function () {
    return view('welcome');
});

In plain English, when we access the root directory of the website, we return to the welcome view. We modify the content of the view:

<pre class="brush:php;toolbar:false">// /resources/views/welcome.blade.php &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt;     &lt;meta charset=&quot;UTF-8&quot;&gt;     &lt;title&gt;Document&lt;/title&gt; &lt;/head&gt; &lt;body&gt;     你好, Laravel &lt;/body&gt; &lt;/html&gt;</pre>

As you can see, when defining the returned view, you can omit the .blade.php suffix. This suffix represents the use of Laravel's Blade template function, which will be introduced later.

Now, when we visit again, it becomes what we defined.

Data transfer

We can also use variables in the view. First, return to the view name variable in the routing function:

<pre class="brush:php;toolbar:false">// /routes/web.php Route::get('/', function () {     $name = &quot;Zen&quot;;    return view('welcome',['name'=&gt;$name]); });</pre>

can also be written as:

// /routes/web.php
Route::get('/', function () {
   $name = "Zen";
   return view('welcome')->with('name',$name);
});

The more common way of writing is to use the provided by php compact function, compact function is to create an array containing variable name and variable value, which is more flexible and simple:

// /routes/web.php
Route::get('/', function () {
    $name = "Zen";
      $age = 99;
      $sex = "男";
      return view('welcome',compact('name','age','sex'));;
});

Display the variable in the view:

// /resources/views/welcome.blade.php
// 省略
<body>
    你好, <?php echo $name?>
</body>

Although the PHP language can be embedded to display the variable, Laravel provides a more concise syntax:

// /resources/views/welcome.blade.php
<body>
   你好, {{ $name }} ,你的年龄是 {{ $age }}, 你的性别是 {{ $sex }}
</body>

or:

// /resources/views/welcome.blade.php
<body>
   你好, {!! $name !!} ,你的年龄是 {!! $age !!}, 你的性别是 {!! $sex !!}
</body>

What is the difference between the two? See the following example:

$data = '<alert>123</alert>'

The output of the two in the view:

  • {{ $data } } will output <alert>123</alert>

  • #{!! $data !!} will Output warning box

In other words:

  • {{variable name}} : escape output

  • {!! Variable name!!}: Native output, such as pictures, links, js codes, etc.


## Routing and View

Main knowledge points:

    Basic process from routing to view
  • Data transfer
  • Let’s take a look at how the last page of the first lecture comes out. Let’s take a look at routing first:
// /routes/web.php
Route::get('/', function () {
    return view('welcome');
});

In plain English, when we access the root directory of the website, we return to the

welcome

view. We modify the content of the view: <pre class="brush:php;toolbar:false">// /resources/views/welcome.blade.php &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt;     &lt;meta charset=&quot;UTF-8&quot;&gt;     &lt;title&gt;Document&lt;/title&gt; &lt;/head&gt; &lt;body&gt;     你好, Laravel &lt;/body&gt; &lt;/html&gt;</pre>As you can see, when defining the returned view, you can omit the

.blade.php

suffix. This suffix represents the use of Laravel's Blade template function, which will be introduced later. Now, when we visit again, it becomes what we defined.

Data transfer

We can also use variables in the view. First, return to the view

name

variable in the routing function: <pre class="brush:php;toolbar:false">// /routes/web.php Route::get('/', function () {     $name = &quot;Zen&quot;;    return view('welcome',['name'=&gt;$name]); });</pre> can also be written as:

// /routes/web.php
Route::get('/', function () {
   $name = "Zen";
   return view('welcome')->with('name',$name);
});

The more common way of writing is to use the

provided by php compact

function, compact function is to create an array containing variable name and variable value, which is more flexible and simple:

// /routes/web.php
Route::get('/', function () {
    $name = "Zen";
      $age = 99;
      $sex = "男";
      return view('welcome',compact('name','age','sex'));;
});
Display the variable in the view:

// /resources/views/welcome.blade.php
// 省略
<body>
    你好, <?php echo $name?>
</body>

Although the PHP language can be embedded to display the variable, Laravel provides a more concise syntax:

// /resources/views/welcome.blade.php
<body>
   你好, {{ $name }} ,你的年龄是 {{ $age }}, 你的性别是 {{ $sex }}
</body>

or:

// /resources/views/welcome.blade.php
<body>
   你好, {!! $name !!} ,你的年龄是 {!! $age !!}, 你的性别是 {!! $sex !!}
</body>

What is the difference between the two? See the following example:

$data = '<alert>123</alert>'

The output of the two in the view:

  • {{ $data } }

    will output <alert>123</alert>

  • #{!! $data !!}
  • will Output warning box

    In other words:

    {{variable name}}
  • : escape output

  • {!! Variable name!!}
  • : Native output, such as pictures, links, js codes, etc.

The above is the detailed content of Laravel 5.4 Getting Started Series: Routing and Views. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn