Main knowledge points of routing and view: the basic process data from routing to view is transmitted. let's take a look at how the last page came out. Let's take a look at the routing: {code...} in the white paper, that is, when we visit the root directory of the website, we will return to the welcome View. let's modify the main knowledge points:
Let's see how the last page came out. Let's take a look at the route:
// /routes/web.phpRoute::get('/', function () { return view('welcome');});
When we access the root directory of the websitewelcomeView:
/// Resources/views/welcome. blade. php
DocumentHello, Laravel
As you can see, when defining the returned view, it can be omitted.blade.phpSuffix. this suffix indicates that the Blade template function of Laravel is used. it will be introduced later.
Now, accessing again is what we define.
Data transmission
We can also use the form of variables in the view. First, return to the view in the routing functionnameVariable:
// /routes/web.phpRoute::get('/', function () { $name = "Zen"; return view('welcome',['name'=>$name]);});
You can also write it as follows:
// /routes/web.phpRoute::get('/', function () { $name = "Zen"; return view('welcome')->with('name',$name);});
A more common method is to use php to providecompactFunction,compactThe function is used to create an includeVariable nameAndVariable valueArray, more flexible and simple:
/// Routes/web. phpRoute: get ('/', function () {$ name = "Zen"; $ age = 99; $ sex = "male"; return view ('Welcome ', compact ('name', 'age', 'sex '));;});
Display the variable in the view:
/// Resources/views/welcome. blade. php // omittedHello,
Although the PHP language can be embedded to display variables, Laravel provides a more concise syntax:
/// Resources/views/welcome. blade. phpHello, {$ name}, your age is {$ age}, and your gender is {$ sex }}
Or:
/// Resources/views/welcome. blade. phpHello ,{!! $ Name !!} , Your age is {!! $ Age !!}, Your gender is {!! $ Sex !!}
What is the difference between the two? let's look at the following example:
$data = '123'
Output of the two in the view:
That is to say:
-
{Variable name }}: Escape output
-
{!! Variable name !!}: Native output, such as slice, link, and js code
Routing and view
Key knowledge points:
Let's see how the last page came out. Let's take a look at the route:
// /routes/web.phpRoute::get('/', function () { return view('welcome');});
When we access the root directory of the websitewelcomeView:
/// Resources/views/welcome. blade. php
DocumentHello, Laravel
As you can see, when defining the returned view, it can be omitted.blade.phpSuffix. this suffix indicates that the Blade template function of Laravel is used. it will be introduced later.
Now, accessing again is what we define.
Data transmission
We can also use the form of variables in the view. First, return to the view in the routing functionnameVariable:
// /routes/web.phpRoute::get('/', function () { $name = "Zen"; return view('welcome',['name'=>$name]);});
You can also write it as follows:
// /routes/web.phpRoute::get('/', function () { $name = "Zen"; return view('welcome')->with('name',$name);});
A more common method is to use php to providecompactFunction,compactThe function is used to create an includeVariable nameAndVariable valueArray, more flexible and simple:
/// Routes/web. phpRoute: get ('/', function () {$ name = "Zen"; $ age = 99; $ sex = "male"; return view ('Welcome ', compact ('name', 'age', 'sex '));;});
Display the variable in the view:
/// Resources/views/welcome. blade. php // omittedHello,
Although the PHP language can be embedded to display variables, Laravel provides a more concise syntax:
/// Resources/views/welcome. blade. phpHello, {$ name}, your age is {$ age}, and your gender is {$ sex }}
Or:
/// Resources/views/welcome. blade. phpHello ,{!! $ Name !!} , Your age is {!! $ Age !!}, Your gender is {!! $ Sex !!}
What is the difference between the two? let's look at the following example:
$data = '123'
Output of the two in the view:
That is to say:
-
{Variable name }}: Escape output
-
{!! Variable name !!}: Native output, such as slice, link, and js code
The above is the entry series of Laravel 5.4: Detailed information about routes and views. For more information, see other related articles in the first PHP community!