Laravel的Responses继承自SymfonyComponentHttpFoundationResponse类,提供了多种方法用于构建HTTP Response。比如View Responses.

视图Views

视图即包含HTML展示界面。Laravel视图通常位于app/views目录中,以.php文件名结尾,比如

  1. <!-- app/views/simple.php -->
  2. <!doctype html>
  3. <htmllang="en">
  4. <head>
  5. <metacharset="UTF-8">
  6. <title>Views!</title>
  7. </head>
  8. <body>
  9. <p>Oh yeah! VIEWS!</p>
  10. </body>
  11. </html>

通过View::make(),很容易在路由中返回

  1. // app/routes.php
  2. Route::get('/',function()
  3. {
  4. returnView::make('simple');
  5. });

传递数据给视图

  1. // app/routes.php
  2. Route::get('/{squirrel}',function($squirrel)
  3. {
  4. $data['squirrel']= $squirrel;
  5. returnView::make('simple', $data);
  6. });

虽然View::make('simple', $data);中传递的是$data,但是视图中并不能使用$data,而是使用的是数组中Key值作为变量名使用,比如数组形式

  1. array('name'=>'Taylor Otwell','status'=>'Code Guru');

在视图中可以访问的是

  1. <?php echo $name;// 值为 'Taylor Otwell' ?>
  2. <?php echo $status;// 值为 'Code Guru' ?>

因此simple.php中用$squirrel访问

  1. <body>
  2. <p>I wish I were a <?php echo $squirrel;?> squirrel!</p>
  3. </body>

还可以以这种形式传递

  1. returnView::make('simple')->with('squirrel','Steve');

在所有视图中同共享同一数据

  1. View::share('name','Steve');

向视图传递子视图

或许你可能想将一个视图放入到另一个视图中。例如,将存放在app/views/child/view.php文件中的子视图传递给另一视图,如下

  1. $view =View::make('greeting')->nest('child','child.view');
  2. $view =View::make('greeting')->nest('child','child.view', $data);

在父视图就可以输出该子视图了

  1. <html>
  2. <body>
  3. <h1>Hello!</h1>
  4. <?php echo $child;?>
  5. </body>
  6. </html>

视图合成器

视图合成器可以是回调函数或者类方法,它们在创建视图时被调用。如果你想在应用程序中,每次创建视图时都为其绑定一些数据,使用视图合成器可以将代码组织到一个地方。

  1. View::composer('profile',function($view)
  2. {
  3. $view->with('count',User::count());
  4. });

每次创建profile视图时,count都会被绑定到视图中。也可以为多个视图同时绑定一个视图合成器

  1. View::composer(array('profile','dashboard'),function($view)
  2. {
  3. $view->with('count',User::count());
  4. });

基于类的视图合成器

  1. View::composer('profile','ProfileComposer');
  2. //视图合成器类可以任意存放,只要能在composer.json文件中指定位置并自动加载即可
  3. classProfileComposer{
  4. publicfunction compose($view)
  5. {
  6. $view->with('count',User::count());
  7. }
  8. }

重定向Redirect

返回一个重定向

  1. returnRedirect::to('user/login');

返回一个重定向至命名路由

  1. returnRedirect::route('login');

返回一个重定向至带有参数的命名路由

  1. returnRedirect::route('profile', array(1));

返回一个重定向至带有命名参数的命名路由

  1. returnRedirect::route('profile', array('user'=>1));

返回一个重定向至控制器Action

  1. returnRedirect::action('HomeController@index');

返回一个重定向至控制器Action并带有参数

  1. returnRedirect::action('UserController@profile', array(1));

返回一个重定向至控制器Action并带有命名参数

  1. returnRedirect::action('UserController@profile', array('user'=>1));

自定义Responses

ViewRedirect都是继承自Response对象,Response对象通常包括body主体,status code状态码,HTTP headersHTTP头部及Cookie等其他有用信息,我们可以自己定义Responses

  1. // app/routes.php
  2. Route::get('custom/response',function()
  3. {
  4. $response =Response::make('Hello world!',200);
  5. $response->headers->set('our key','our value');
  6. return $response;
  7. });

Laravel的Response继承自Symfony HTTPFoundation/Response组件,因此基本上HTTPFoundation/Response的API都可以拿来用。

JSON Response

创建一个JSON Response

  1. // app/routes.php
  2. Route::get('markdown/response',function()
  3. {
  4. $data = array('iron','man','rocks');
  5. returnResponse::json($data);
  6. });

文件下载Response

  1. // app/routes.php
  2. Route::get('file/download',function()
  3. {
  4. $file ='path_to_my_file.pdf';
  5. returnResponse::download($file);
  6. });

Response::download()有三个参数可选,第二个参数为状态码,第三个参数为HTTP头部

  1. returnResponse::download($file,418, array('iron','man'));

Blade模版

使用模版可以简化HTML页面编写,避免在HTML中夹杂混乱的PHP语言。Blade模版使用.blade.php为后缀,使用如下语法

变量输出

  1. <p></p>

script脚本

  1. <p>}</p>

结构控制语法

  1. @if($something)
  2. <p>Somethingistrue!</p>
  3. @else
  4. <p>Somethingisfalse!</p>
  5. @endif
  6. @foreach($manyThings as $thing)
  7. <p></p>
  8. @endforeach
  9. @for ($i = 0; $i < 999; $i++)
  10. <p>Even red pandas, aren't enough!</p>
  11. @endfor
  12. @while(isPretty($kieraKnightly))
  13. <p>This loop probably won't ever end.</p>
  14. @endwhile

模版继承

  1. <!-- app/views/layouts/base.blade.php -->
  2. <!doctype html>
  3. <htmllang="en">
  4. <head>
  5. <metacharset="UTF-8">
  6. <title></title>
  7. @section('head')
  8. <linkrel="stylesheet"href="style.css"/>
  9. @section('head')
  10. </head>
  11. <body>
  12. @yield('body')
  13. </body>
  14. </html>

@section('head')@section('head')之间的内容可以被子模版覆盖或继承,使用@parent将继承抚摸版的内容并添加额外信息。子模版如下:

  1. <!-- app/views/home.blade.php -->
  2. @extends('layouts.base')
  3. @section('head')
  4. @parent
  5. <linkrel="stylesheet"href="another.css"/>
  6. @stop
  7. @section('body')
  8. <h1>Hurray!</h1>
  9. <p>We have a template!</p>
  10. @stop

编译后HTML文件为

  1. <!doctype html>
  2. <htmllang="en">
  3. <head>
  4. <metacharset="UTF-8">
  5. <title></title>
  6. <linkrel="stylesheet"href="style.css"/>
  7. <linkrel="stylesheet"href="another.css"/>
  8. </head>
  9. <body>
  10. <h1>Hurray!</h1>
  11. <p>We have a template!</p>
  12. </body>
  13. </html>

结束

web应用归根结底是一个请求-响应模式,通过分析请求,返回特定的响应,其他所有的逻辑都是基于此展开。