We create a new route in Routes.php
Copy code The code is as follows:
Route::get('about', 'PagesController@about');
You will get an error when browsing in the browser. The error message is just a prompt message and lacks details. It's ok in the production environment, but we hope to get detailed information during the development stage.
Find the .env file in the root directory of the project and modify it
Copy code The code is as follows:
APP_DEBUG=true
This will display a detailed error message, PagesController does not exist. But in the production environment it must be set to false
We can create a new controller manually, but a faster way is to use the generator provided by laravel. Run in the current project directory from the command line:
Copy code The code is as follows:
php artisan
You can see the functions provided by laravel.
Copy code The code is as follows:
php artisan make:controller PagesController
ok, PagesController.php
is generated under app->http->controller<?php namespace App\Http\Controllers; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class PagesController extends Controller { /** * Display a listing of the resource. * * @return Response */ public function index() { // } /** * Show the form for creating a new resource. * * @return Response */ public function create() { // } /** * Store a newly created resource in storage. * * @return Response */ public function store() { // } /** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { // } }
The controller generated in this way contains all the required RESTful methods, we can simplify it. Delete the generated PagesController.php and run it on the command line:
Copy code The code is as follows:
php artisan make:controller PagesController --plain
Take another look at the generated results
Copy code The code is as follows:
use AppHttpRequests;
use AppHttpControllersController;
use IlluminateHttpRequest;
class PagesController extends Controller {
//
}
Basically an empty controller, we need to create all the methods ourselves.
If you want to know what parameters we can execute on the command line, you can run the following command to view the help
Copy code The code is as follows:
php artisan help make:controller
ok, you can often use the help command to help you understand these parameters.
Establish the about method in PagesController.
Copy code The code is as follows:
public function about() {
return 'About Page';
}
Check the results in the browser, the error disappears and simple information is returned.
Back to view
Of course we want to return the html document and modify the return of the about method:
Copy code The code is as follows:
public function about() {
return view('pages.about');
}
Note: The returned result is pages.about , which means the about.balde.php file in the pages subdirectory within the views subdirectory. Let’s create the resourcesviewspagesabout.balde.php file
Copy code The code is as follows:
That's it. Run your browser to see,😄
Transfer data to the view
Modify PagesController.php
Copy code The code is as follows:
public function about() {
$name = 'Zhang Jinlgin';
return view('pages.about')->with('name', $name);
}
Modify our view file about.blade.php
Copy code The code is as follows:
Bingo, check the results.
The laravel we use uses blade templates, we can use this benefit to modify the view:
Copy code The code is as follows:
Looks better. In blade, {{}} escapes the semantics of html. Let me modify a piece of data:
Copy code The code is as follows:
$name = 'Zhang Jinlgin';
View the results and find that all html elements have been escaped. But if you don’t need to escape html, you can use {!! !!} to modify the view:
Copy code The code is as follows:
Look at the results again,👌
The above is the entire content of this article. I hope it will help everyone master Laravel5.