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:
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.
We can also use variables in the view. First, return to the view name
variable in the routing function:
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
{{variable name}} : escape output
{!! Variable name!!}: Native output, such as pictures, links, js codes, etc.
## Routing and View
// /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: <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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
你好, Laravel
</body>
</html></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
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
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 = "Zen";
return view('welcome',['name'=>$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); });
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'));;
});
// /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:
will output <alert>123</alert>
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!