Laravel 5 Basics (iii)-Transferring data to a view

Source: Internet
Author: User
    • We create a new route in the routes.php
Route::get('about', 'PagesController@about');

Browsing in the browser will get an error, the error message is just a hint, the lack of detail in the production environment It ' OK, but the development phase we want to get detailed information.

    • Locate the. env file in the root directory of the project, modify
APP_DEBUG=true

This will display a detailed error message that Pagescontroller does not exist. But must be set to false in the production environment

    • We can create a new controller manually, but the quicker way is to take advantage of the generator provided by Laravel. Run in the current project directory on the command line:
php artisan

You can see the features provided by Laravel.

php artisan make:controller PagesController

OK, app->http->controller pagescontroller.php is generated below.

!--? 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 Res Ponse */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 Resou Rce. * * @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) {//}} 

这样生成的controller包含了全部所需要的RESTful方法,我们可以简化一下。删除生成的PagesController.php,在命令行运行:

php artisan make:controller PagesController --plain

再看一下生成的结果

 

基本上是一个空的controller,所有的方法我们需要自己创建。

如果你想知道到底有什么参数我们可以在命令行执行,你可以运行下面的命令来查看帮助

php artisan help make:controller

ok, 你可以经常使用help命令来帮助你了解这些参数。

    • 在PagesController中建立about方法。
public function about() { return 'About Page'; }

在浏览器冲查看结果,错误消失,返回简单的信息。

    • 返回视图

我们当然希望返回html文档,修改about方法的返回:

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

注意:返回的结果是 pages.about ,这表示在 views 子目录中的 pages 子目录中的 about.balde.php 文件。让我们创建 resources\views\pages\about.balde.php 文件

  Document

About

That's it. 运行浏览器查看吧,??

    • 向视图传送数据

修改PagesController.php

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

修改我们的视图文件 about.blade.php

About

Bingo,查看结果吧。

我们使用的laravel使用了blade模板,我们可以利用这个好处修改视图:

About {{ $name }}

看起来更好了,在blade中,{{}}是转义html的语义的,让我来修改一个数据:

$name = 'Zhang Jinlgin';

查看结果,发现所有的html元素都被转义了。但是如果不需要转义html,可以使用 {!! !!},修改视图:

About {!! $name !!}

再看结果,??

以上就介绍了Laravel 5 基础(三)- 向视图传送数据,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

  • 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.