route.php 1.16 KB
Newer Older
Taylor Otwell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php namespace Laravel\CLI\Tasks;

use Laravel\URI;
use Laravel\Request;
use Laravel\Routing\Router;

class Route extends Task {

	/**
	 * Execute a route and dump the result.
	 *
	 * @param  array  $arguments
	 * @return void
	 */
Taylor Otwell committed
15
	public function call($arguments = array())
Taylor Otwell committed
16 17 18 19 20 21 22 23
	{
		if ( ! count($arguments) == 2)
		{
			throw new \Exception("Please specify a request method and URI.");
		}

		// First we'll set the request method and URI in the $_SERVER array,
		// which will allow the framework to retrieve the proper method
Taylor Otwell committed
24
		// and URI using the URI and Request classes.
Taylor Otwell committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
		$_SERVER['REQUEST_METHOD'] = strtoupper($arguments[0]);

		$_SERVER['REQUEST_URI'] = $arguments[1];

		$this->route();

		echo PHP_EOL;
	}

	/**
	 * Dump the results of the currently established route.
	 *
	 * @return void
	 */
	protected function route()
	{
		// We'll call the router using the method and URI specified by
		// the developer on the CLI. If a route is found, we will not
		// run the filters, but simply dump the result.
		$route = Router::route(Request::method(), URI::current());

		if ( ! is_null($route))
		{
			var_dump($route->response());
		}
		else
		{
			echo '404: Not Found';
		}
	}

}