[转]laravel 4之视图及Responses
Laravel的Responses继承自Symfony\Component\HttpFoundation\Response类,提供了多种方法用于构建HTTP Response。比如View Responses.
视图Views
视图即包含HTML展示界面。Laravel视图通常位于app/views目录中,以.php文件名结尾,比如
<!-- app/views/simple.php --><!doctype html><htmllang="en"><head><metacharset="UTF-8"><title>Views!</title></head><body><p>Oh yeah! VIEWS!</p></body></html>
通过View::make(),很容易在路由中返回
// app/routes.phpRoute::get('/',function(){returnView::make('simple');});
传递数据给视图
// app/routes.phpRoute::get('/{squirrel}',function($squirrel){$data['squirrel']= $squirrel;returnView::make('simple', $data);});
虽然View::make('simple', $data);中传递的是$data,但是视图中并不能使用$data,而是使用的是数组中Key值作为变量名使用,比如数组形式
array('name'=>'Taylor Otwell','status'=>'Code Guru');
在视图中可以访问的是
<?php echo $name;// 值为 'Taylor Otwell' ?><?php echo $status;// 值为 'Code Guru' ?>
因此simple.php中用$squirrel访问
<body><p>I wish I were a <?php echo $squirrel;?> squirrel!</p></body>
还可以以这种形式传递
returnView::make('simple')->with('squirrel','Steve');
在所有视图中同共享同一数据
View::share('name','Steve');
向视图传递子视图
或许你可能想将一个视图放入到另一个视图中。例如,将存放在app/views/child/view.php文件中的子视图传递给另一视图,如下
$view =View::make('greeting')->nest('child','child.view');$view =View::make('greeting')->nest('child','child.view', $data);
在父视图就可以输出该子视图了
<html><body><h1>Hello!</h1><?php echo $child;?></body></html>
视图合成器
视图合成器可以是回调函数或者类方法,它们在创建视图时被调用。如果你想在应用程序中,每次创建视图时都为其绑定一些数据,使用视图合成器可以将代码组织到一个地方。
View::composer('profile',function($view){$view->with('count',User::count());});
每次创建profile视图时,count都会被绑定到视图中。也可以为多个视图同时绑定一个视图合成器
View::composer(array('profile','dashboard'),function($view){$view->with('count',User::count());});
基于类的视图合成器
View::composer('profile','ProfileComposer');//视图合成器类可以任意存放,只要能在composer.json文件中指定位置并自动加载即可classProfileComposer{publicfunction compose($view){$view->with('count',User::count());}}
重定向Redirect
返回一个重定向
returnRedirect::to('user/login');
返回一个重定向至命名路由
returnRedirect::route('login');
返回一个重定向至带有参数的命名路由
returnRedirect::route('profile', array(1));
返回一个重定向至带有命名参数的命名路由
returnRedirect::route('profile', array('user'=>1));
返回一个重定向至控制器Action
returnRedirect::action('HomeController@index');
返回一个重定向至控制器Action并带有参数
returnRedirect::action('UserController@profile', array(1));
返回一个重定向至控制器Action并带有命名参数
returnRedirect::action('UserController@profile', array('user'=>1));
自定义Responses
View跟Redirect都是继承自Response对象,Response对象通常包括body主体,status code状态码,HTTP headersHTTP头部及Cookie等其他有用信息,我们可以自己定义Responses
// app/routes.phpRoute::get('custom/response',function(){$response =Response::make('Hello world!',200);$response->headers->set('our key','our value');return $response;});
Laravel的Response继承自Symfony HTTPFoundation/Response组件,因此基本上HTTPFoundation/Response的API都可以拿来用。
JSON Response
创建一个JSON Response
// app/routes.phpRoute::get('markdown/response',function(){$data = array('iron','man','rocks');returnResponse::json($data);});
文件下载Response
// app/routes.phpRoute::get('file/download',function(){$file ='path_to_my_file.pdf';returnResponse::download($file);});
Response::download()有三个参数可选,第二个参数为状态码,第三个参数为HTTP头部
returnResponse::download($file,418, array('iron','man'));
Blade模版
使用模版可以简化HTML页面编写,避免在HTML中夹杂混乱的PHP语言。Blade模版使用.blade.php为后缀,使用如下语法
变量输出
<p></p>
script脚本
<p>}</p>
结构控制语法
@if($something)<p>Somethingistrue!</p>@else<p>Somethingisfalse!</p>@endif@foreach($manyThings as $thing)<p></p>@endforeach@for ($i = 0; $i < 999; $i++)<p>Even red pandas, aren't enough!</p>@endfor@while(isPretty($kieraKnightly))<p>This loop probably won't ever end.</p>@endwhile
模版继承
<!-- app/views/layouts/base.blade.php --><!doctype html><htmllang="en"><head><metacharset="UTF-8"><title></title>@section('head')<linkrel="stylesheet"href="style.css"/>@section('head')</head><body>@yield('body')</body></html>
@section('head')和@section('head')之间的内容可以被子模版覆盖或继承,使用@parent将继承抚摸版的内容并添加额外信息。子模版如下:
<!-- app/views/home.blade.php -->@extends('layouts.base')@section('head')@parent<linkrel="stylesheet"href="another.css"/>@stop@section('body')<h1>Hurray!</h1><p>We have a template!</p>@stop
编译后HTML文件为
<!doctype html><htmllang="en"><head><metacharset="UTF-8"><title></title><linkrel="stylesheet"href="style.css"/><linkrel="stylesheet"href="another.css"/></head><body><h1>Hurray!</h1><p>We have a template!</p></body></html>
结束
web应用归根结底是一个请求-响应模式,通过分析请求,返回特定的响应,其他所有的逻辑都是基于此展开。
邮箱:steven9801@163.com
QQ: 48039387
浙公网安备 33010602011771号