Phill Sparks

Fork me on GitHub

Service Bundle

Simplified service routing for Laravel.

##Install with Artisan

php artisan bundle:install service

##Bundle Registration

'service' => array(
	'auto' => true, // Load some default services
	'autoloads' => array(
		'map' => array(
			'Service' => '(:bundle)/service.php',
		),
	),
),

##Method A (Recommended)

The Service class extends the Laravel\Response class and provides some extended Route methods too. The methods all follow the same pattern.

Service::get    (string $route, string[] $allowed, Closure|array $action)
Service::put    (string $route, string[] $allowed, Closure|array $action)
Service::post   (string $route, string[] $allowed, Closure|array $action)
Service::delete (string $route, string[] $allowed, Closure|array $action)
Service::any    (string $route, string[] $allowed, Closure|array $action)
ParameterTypeDescription
$routestring

A normal route.

Note: The Service class currently supports only a string here.

$allowedstring[]

An array of the service types you wish to allow for this route.

The type is auto-detected based on this list, if the request is for an unspecified type, or the type is not a registered service then a 404 error response will be returned.

The first type in the $allowed list is the default type, used for the raw route. If you do not want a default pass null as the first type.

$actionClosure|array

Just like a route action except that the first parameter to the Closure will be a Service, any route parameters will be passed in after the service.

Any response returned from the Closure will be returned by the route, if you do not return anything then the service handler will kick in and generate an appropriate response. This is useful for handling HTML where you will want to configure the view yourself.

###Example

Service::get('user/(:any)', array('html', 'json', 'xml'), function(Service $service, $slug)
{
	$service->data['user'] = User::where_slug($slug);
	
	// Handle HTML type
	if ($service->type == 'html')
	{
		return View::make('user.show', array(
			'user' => $service->data['user']
		));
	}
});

##Method B (Alternative)

The Service class also provides a way of using Services within existing routes.

Service::respond (string|null $type, string[] $allowed, Closure $callback, array $args = array())
ParameterTypeDescription
$typestring|null

An override type, you can use this to force a type (useful for using file extensions).

If you pass null here then the Service detector will kick in and guess the route based on the allowed list.

Note: You cannot force a $type that is not in the $allowed list.

$allowedstring[]

The same as Service::get()

$callbackClosure

A Closure with a single parameter, the Service.

If you want to pass data into your closure then "use" and $args are your friends.

Everything else about the Closure is the same as Method A.

$argsarray

An optional array of parameters to be passed to $callback after the Service.

###Example

Route::get(array('user/(:any)', 'user/(:any).(json|xml)'), function($slug, $type = null)
{
	return Service::respond($type, array('html', 'json', 'xml'), function(Service $service) use ($slug)
	{
		$service->data['user'] = User::where_slug($slug);

		// Handle HTML type
		if ($service->type == 'html')
		{
			return View::make('user.show', array(
				'user' => $service->data['user']
			));
		}
	});
});