Laravel 5.4 Getting Started Series: Routing and Views

巴扎黑
Release: 2023-03-07 13:22:01
Original
2507 people have browsed it

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');
});
Copy after login
Copy after login

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:

<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div>

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:

<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// /routes/web.php Route::get('/', function () {     $name = &quot;Zen&quot;;    return view('welcome',['name'=&gt;$name]); });</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div>

can also be written as:

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

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'));;
});
Copy after login
Copy after login

Display the variable in the view:

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

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>
Copy after login
Copy after login

or:

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

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

$data = '<alert>123</alert>'
Copy after login
Copy after login

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');
});
Copy after login
Copy after login

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: <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div>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: <div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// /routes/web.php Route::get('/', function () {     $name = &quot;Zen&quot;;    return view('welcome',['name'=&gt;$name]); });</pre><div class="contentsignin">Copy after login</div></div><div class="contentsignin">Copy after login</div></div> can also be written as:

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

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'));;
});
Copy after login
Copy after login
Display the variable in the view:

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

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>
Copy after login
Copy after login

or:

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

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

$data = '<alert>123</alert>'
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template