: 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.
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 ,??
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.