Laravel5 basics (III)-transfer data to views

Source: Internet
Author: User
: This article mainly introduces Laravel5 basics (III)-transfer data to views. For more information about PHP tutorials, see.
  • Create a new route in Routes. php
Route::get('about', 'PagesController@about');

An error is returned when browsing in the browser. the error message is only a prompt message, but the details are missing. in the production environment, It is 'OK', but we want to get details in the development stage.

  • Find the. env file in the root directory of the project and modify it.
APP_DEBUG=true

This displays the detailed error message. PagesController does not exist. However, you must set it to false in the production environment.

  • We can create a controller manually, but the faster way is to use the generator provided by laravel. Run:
php artisan

The functions provided by laravel are displayed.

php artisan make:controller PagesController

OK, inapp->http->controllerThe following generate PagesController. php


  

The generated controller contains all the required RESTful methods, which can be simplified. Delete the generated PagesController. php and run the following command on the command line:

php artisan make:controller PagesController --plain

Let's take a look at the generated results.

 

Basically, it is an empty controller. we need to create all the methods ourselves.

If you want to know what parameters are available, we can execute them in the command line. you can run the following command to view help

php artisan help make:controller

OK. You can often use the help command to help you understand these parameters.

  • Create the about method in PagesController.
public function about() { return 'About Page'; }

Check the result in the browser. the error disappears and a simple message is returned.

  • Back to view

We certainly want to return the html document and modify the return of the about method:

public function about() { return view('pages.about'); }

Note: The Returned result ispages.about, Which indicatesviewsSubdirectorypagesSubdirectoryabout.balde.phpFile. Let's createresources\views\pages\about.balde.phpFile

  DocumentAbout

That's it. run the browser to view it, right ,??

  • Transfer data to views

Modify PagesController. php

public function about() { $name = 'Zhang Jinlgin'; return view('pages.about')->with('name', $name); }

Modify our view file about. blade. php

About 

Bingo. check the result.

Laravel we use uses the blade template. we can use this to modify the view:

About {{ $name }}

It looks better. in blade, {} is the meaning of html. let me modify a data:

$name = 'Zhang Jinlgin';

View the results and find that all html elements are escaped. However, if you do not need to escape html, you can use {!! !!}, Modify view:

About {!! $name !!}

Then let's look at the results ,??

The above introduces Laravel 5 basics (III)-transfer data to views, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.