视图 & Response


基本Response

从路由中返回字符串

  1. Route::get('/', function()
  2. {
  3. return 'Hello World';
  4. });

创建自定义Response

Response类继承自Symfony\Component\HttpFoundation\Response类,提供了多种方法用于构建HTTP Response。

  1. $response = Response::make($contents, $statusCode);
  2. $response->header('Content-Type', $value);
  3. return $response;

在Response中添加Cookie

  1. $cookie = Cookie::make('name', 'value');
  2. return Response::make($content)->withCookie($cookie);

重定向

返回一个重定向

  1. return Redirect::to('user/login');

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

  1. return Redirect::route('login');

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

  1. return Redirect::route('profile', array(1));

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

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

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

  1. return Redirect::action('HomeController@index');

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

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

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

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

视图

视图通常包含应用中的HTML代码,为分离表现层与控制器和业务逻辑提供了便利。视图存放于app/views目录。

一个简单视图案例:

  1. <!-- View stored in app/views/greeting.php -->
  2. <html>
  3. <body>
  4. <h1>Hello, <?php echo $name; ?></h1>
  5. </body>
  6. </html>

通过如下方法来返回该视图到浏览器:

  1. Route::get('/', function()
  2. {
  3. return View::make('greeting', array('name' => 'Taylor'));
  4. });

传递给View::make方法的第二个参数是一个数组,它将被传递给视图。

传递数据给视图

  1. $view = View::make('greeting', $data);
  2. $view = View::make('greeting')->with('name', 'Steve');

在上面的案例中,$name变量在视图内是可以访问的,其值为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. });

如果你更喜欢使用基于类的视图合成器,IoC container可以提供更多便利,如下所示:

  1. View::composer('profile', 'ProfileComposer');

视图合成器类定义如下:

  1. class ProfileComposer {
  2. public function compose($view)
  3. {
  4. $view->with('count', User::count());
  5. }
  6. }

注意,没有规定视图合成器类存放在哪里。因此,你可以任意存放,只要能在composer.json文件中指定位置并自动加载即可。

View Creators

View creators work almost exactly like view composers; however, they are fired immediately when the view is instantiated. To register a view creator, simple use the creator method:

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

特殊Response

创建一个JSON Response

  1. return Response::json(array('name' => 'Steve', 'state' => 'CA'));

创建一个JSONP Response

  1. return Response::json(array('name' => 'Steve', 'state' => 'CA'))->setCallback(Input::get('callback'));

创建一个文件下载Response

  1. return Response::download($pathToFile);
  2. return Response::download($pathToFile, $status, $headers);