Home  >  Article  >  Backend Development  >  Laravel 5 Basics (3) - Passing data to the view

Laravel 5 Basics (3) - Passing data to the view

WBOY
WBOYOriginal
2016-08-08 09:26:53944browse
  • We create a new route in Routes.php
<code>Route::get('about', 'PagesController@about');</code>

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
<code>APP_DEBUG=true</code>

This will display the detailed error message, PagesController does not exist. But it must be set to false in the production environment

  • 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:
<code>php artisan</code>

You can see the functions provided by laravel.

<code>php artisan make:controller PagesController</code>

ok, PagesController.php is generated under app->http->controller

<code><?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)
	{
		//
	}

}</code>

The controller generated in this way contains all the required RESTful methods, we can simplify it a bit. Delete the generated PagesController.php and run it from the command line:

<code>php artisan make:controller PagesController --plain</code>

Look at the generated results again

<code><?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class PagesController extends Controller {

	//

}</code>

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

<code>php artisan help make:controller</code>

ok, you can often use the help command to help you understand these parameters.

  • Establish the about method in PagesController.
<code>	public function about() {
        return &#39;About Page&#39;;
    }</code>

View 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:

<code>	public function about() {
        return view(&#39;pages.about&#39;);
    }</code>

Note: The result returned is pages.about , which represents the about.balde.php file in the pages subdirectory of the views subdirectory. Let’s create the resourcesviewspagesabout.balde.php file

<code><!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<h1>About</h1>
</body>
</html></code>

That's it. Run your browser to see it,??

  • Transfer data to the view

Modify PagesController.php

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

Modify our view file about.blade.php

<code><h1>About <?= $name ?></h1></code>

Bingo, check the results.

The laravel we use uses blade template, we can use this benefit to modify the view:

<code><h1>About {{ $name }}</h1></code>

It looks better. In blade, {{}} escapes the semantics of html. Let me modify a piece of data:

<code>$name = '<span style="color: red">Zhang Jinlgin</span>';</code>

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:

<code><h1>About {!! $name !!}</h1></code>

Look at the results again,??

The above has introduced the basics of Laravel 5 (3) - transmitting data to the view, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn