惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Zhangjinglin

Laravel 5 基础(十二)- 认证 Laravel 5 基础(十一)- Eloquent 关系 Laravel 5 基础(十一)- 子视图和表单复用 Laravel 5 基础(十一)- 表单验证 Laravel 5 基础(十)- 日期,Mutator 和 Scope Laravel 5 基础(九)- 表单 Laravel 5 基础(八)- 模型、控制器、视图基础流程 Laravel 5 基础(七)- Eloquent (laravel 的ORM) Laravel 5 基础(六)- 数据库迁移(Migrations) Laravel 5 基础(五)- 环境与配置 Laravel 5 基础(四)- Blade 简介 Laravel 5 基础(三)- 向视图传送数据(续) Laravel 5 基础(二)- 路由、控制器和视图简介 Laravel 5 基础(一)- Laravel入门和新建项目 Android 随机铃声管理器 ubuntu netbeans 字体解决 Mac OS 10.6 原版安装 git 强制恢复到某一版本 混乱中生存 - Zhangjinglin
Laravel 5 基础(三)- 向视图传送数据
Zhangjinglin · 2015-03-31 · via 博客园 - Zhangjinglin
  • 我们在Routes.php中新建一个路由
Route::get('about', 'PagesController@about');

在浏览器中浏览会获得一个错误,错误信息仅仅是一个提示信息,缺少细节,在生产环境 It' ok,但是开发阶段我们希望获得详细信息。

  • 在项目的根目录找到 .env 文件,修改
APP_DEBUG=true

这将显示详细的错误信息,PagesController 不存在。但在生产环境一定要设置为 false

  • 我们可以手工新建控制器,但更快的方式是利用 laravel 提供的生成器。在命令行当前项目目录中运行:
php artisan

可以看到laravel提供的功能。

php artisan make:controller PagesController

ok,在 app->http->controller 下面生成了 PagesController.php

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

}

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

php artisan make:controller PagesController --plain

再看一下生成的结果

<?php namespace App\Http\Controllers;

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

use Illuminate\Http\Request;

class PagesController extends Controller {

	//

}

基本上是一个空的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 文件

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

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

  • 向视图传送数据

修改PagesController.php

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

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

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

Bingo,查看结果吧。

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

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

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

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

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

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

再看结果,👌