Home >Backend Development >PHP Tutorial >Laravel 5 framework learning - transferring data to the view, laravel framework_PHP tutorial
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:
a1ae7c7686bc74b8cffb416e553f078b
49099650ebdc5f3125501fa170048923
93f0f5c25f18dab9d176bd4f6de5d30e
1fc2df4564f5324148703df3b6ed50c1
b2386ffb911b14667cb8f0f91ea547a7Document6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
4a249f0d628e2318394fd9b75b4636b1About473f0a7621bec819994bb5020d29372a
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
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:
4a249f0d628e2318394fd9b75b4636b1About c337a99dc110a437710c0953eec4f415473f0a7621bec819994bb5020d29372a
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:
4a249f0d628e2318394fd9b75b4636b1About {{ $name }}473f0a7621bec819994bb5020d29372a
Looks better. In blade, {{}} escapes the semantics of html. Let me modify a piece of data:
Copy code The code is as follows:
$name = '85d0aa4600ad874af06e1b4812aac0c7Zhang Jinlgin54bdf357c58b8a65c66d7c19c8e4d114';
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:
4a249f0d628e2318394fd9b75b4636b1About {!! $name !!}473f0a7621bec819994bb5020d29372a
Look at the results again,👌
The above is the entire content of this article. I hope it will help everyone master Laravel5.