Laravel5.4 entry Series: routes and views

Source: Internet
Author: User
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:

  • Basic process from route to view

  • Data transmission

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:

  • {{ $data }}Will output123

  • {!! $data !!}A warning box is displayed.

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:

  • Basic process from route to view

  • Data transmission

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:

  • {{ $data }}Will output123

  • {!! $data !!}A warning box is displayed.

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.