Commit 15cde607 authored by Taylor Otwell's avatar Taylor Otwell

refactoring redirect and response classes.

parent f79dd1ba
...@@ -16,6 +16,12 @@ return array( ...@@ -16,6 +16,12 @@ return array(
}), }),
'laravel.download' => array('singleton' => true, 'resolver' => function($container)
{
return new Download($container->resolve('laravel.file'));
}),
'laravel.file' => array('singleton' => true, 'resolver' => function($container) 'laravel.file' => array('singleton' => true, 'resolver' => function($container)
{ {
return new File($container->resolve('laravel.config')->get('mimes')); return new File($container->resolve('laravel.config')->get('mimes'));
...@@ -57,6 +63,14 @@ return array( ...@@ -57,6 +63,14 @@ return array(
}), }),
'laravel.response' => array('singleton' => true, 'resolver' => function($container)
{
require_once SYS_PATH.'response'.EXT;
return new Response_Factory($container->resolve('laravel.view'));
}),
'laravel.router' => array('singleton' => true, 'resolver' => function($container) 'laravel.router' => array('singleton' => true, 'resolver' => function($container)
{ {
return new Routing\Router($container->resolve('laravel.request'), require APP_PATH.'routes'.EXT, CONTROLLER_PATH); return new Routing\Router($container->resolve('laravel.request'), require APP_PATH.'routes'.EXT, CONTROLLER_PATH);
......
<?php namespace Laravel;
class Download extends Response {
/**
* Create a new download generator instance.
*
* @param File $file
* @return void
*/
public function __construct(File $file)
{
$this->file = $file;
}
/**
* Create a new download response instance.
*
* @param string $path
* @param string $name
* @return Response
*/
public function of($path, $name = null)
{
if (is_null($name)) $name = basename($path);
$response = parent::__construct($this->file->get($path));
$response->header('Content-Description', 'File Transfer');
$response->header('Content-Type', $this->file->mime($this->file->extension($path)));
$response->header('Content-Disposition', 'attachment; filename="'.$name.'"');
$response->header('Content-Transfer-Encoding', 'binary');
$response->header('Expires', 0);
$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response->header('Pragma', 'public');
$response->header('Content-Length', $this->file->size($path));
return $response;
}
}
\ No newline at end of file
...@@ -9,23 +9,15 @@ class Response_Factory { ...@@ -9,23 +9,15 @@ class Response_Factory {
*/ */
private $view; private $view;
/**
* The file manager instance.
*
* @var File
*/
private $file;
/** /**
* Create a new response factory instance. * Create a new response factory instance.
* *
* @param File $file * @param File $file
* @return void * @return void
*/ */
public function __construct(View_Factory $view, File $file) public function __construct(View_Factory $view)
{ {
$this->view = $view; $this->view = $view;
$this->file = $file;
} }
/** /**
...@@ -41,36 +33,15 @@ class Response_Factory { ...@@ -41,36 +33,15 @@ class Response_Factory {
} }
/** /**
* Create a new download response instance. * Create a new response instance containing a view.
*
* <code>
* // Return a download response for a given file
* return new Download('path/to/image.jpg');
*
* // Return a download response for a given file and assign a name
* return new Download('path/to/image.jpg', 'you.jpg');
* </code>
* *
* @param string $path * @param string $view
* @param string $name * @param array $data
* @return Response * @return Response
*/ */
public function download($path, $name = null) public function view($view, $data = array())
{ {
if (is_null($name)) $name = basename($path); return new Response($this->view->make($view, $data));
$response = new Response($this->file->get($path));
$response->header('Content-Description', 'File Transfer');
$response->header('Content-Type', $this->file->mime($this->file->extension($path)));
$response->header('Content-Disposition', 'attachment; filename="'.$name.'"');
$response->header('Content-Transfer-Encoding', 'binary');
$response->header('Expires', 0);
$response->header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
$response->header('Pragma', 'public');
$response->header('Content-Length', $this->file->size($path));
return $response;
} }
/** /**
...@@ -80,11 +51,6 @@ class Response_Factory { ...@@ -80,11 +51,6 @@ class Response_Factory {
* *
* Note: The specified error code should correspond to a view in your views/error directory. * Note: The specified error code should correspond to a view in your views/error directory.
* *
* <code>
* // Return a 404 error response
* return new Error('404');
* </code>
*
* @param int $code * @param int $code
* @param array $data * @param array $data
* @return void * @return void
...@@ -233,11 +199,6 @@ class Response { ...@@ -233,11 +199,6 @@ class Response {
/** /**
* Add a header to the response. * Add a header to the response.
* *
* <code>
* // Add a "location" header to a response
* $response->header('Location', 'http://google.com');
* </code>
*
* @param string $name * @param string $name
* @param string $value * @param string $value
* @return Response * @return Response
...@@ -248,4 +209,16 @@ class Response { ...@@ -248,4 +209,16 @@ class Response {
return $this; return $this;
} }
/**
* Set the response status code.
*
* @param int $status
* @return Response
*/
public function status($status)
{
$this->status = $status;
return $this;
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment