Skip to content

Instantly share code, notes, and snippets.

@nathggns
Created April 2, 2015 20:40
Show Gist options
  • Save nathggns/82bf0c6fdebfd6fbdad2 to your computer and use it in GitHub Desktop.
Save nathggns/82bf0c6fdebfd6fbdad2 to your computer and use it in GitHub Desktop.
Named Route Helper for Laravel
<?php
/**
*
* Nathaniel Higgins
*
* Add some easy helpers for adding named routes
*
* Use as follows:
*
* get('/', named('home', 'HomeController@index'));
* post('/help', named('help', function() {
* // ...
* });
*
* named_group('auth.', function() {
* Route::group(['prefix' => 'auth'], function() {
* get('/login', named('login', 'AuthController@login'));
* });
* });
*
* These are just aliases for \App\Helper\named
*/
namespace App\Helpers {
/**
* Class NamedRouteHelper
* @package App\Helpers
*
* The main workhorse of this system
*/
class NamedRouteHelper {
/**
* @var string
*
* The current prefix for named routes.
*
* @see group
*/
private $current_prefix = '';
/**
* @param $name
* @param $action \Closure|string
* @return array
*
* Get an array for a named route using either a controller or a closure
*/
public function named($name, $action) {
return array_merge(
[ 'as' => $this->current_prefix . $name ],
$action instanceof \Closure ?
[$action] :
['uses' => $action]
);
}
/**
* @param $prefix
* @param callable $callback
*
* Create a group where all route names have a prefix.
*/
public function group($prefix, \Closure $callback) {
$old_prefix = $this->current_prefix;
$this->current_prefix .= $prefix;
call_user_func($callback);
$this->current_prefix = $old_prefix;
}
}
}
/**
* This is to hide a yucky static class version of the actual helper
*
* @todo Should probably use a facade? Can some of you Laravel-y people help me out with this? I managed to but it
* broke autocomplete?
*
* @note Ignore this. It just passes on all of its arguments to a singleton
*/
namespace ___AppHelperDefaultVars {
use App\Helpers\NamedRouteHelper as Helper;
class NamedRouteHelper {
private static $defaultHelper;
static function named($name, $action, Helper $helper = null) {
$helper = static::getDefaultHelper($helper);
return $helper->named($name, $action);
}
static function group($prefix, \Closure $callback, Helper $helper = null) {
$helper = static::getDefaultHelper($helper);
$helper->group($prefix, $callback);
}
private static function getDefaultHelper(Helper $overwrite = null) {
if ($overwrite) {
return $overwrite;
}
if (!static::$defaultHelper) {
static::$defaultHelper = new Helper();
}
return static::$defaultHelper;
}
}
}
/**
* These are our little helper functions
*/
namespace {
use App\Helpers\NamedRouteHelper as Helper;
if (!function_exists('named')) {
function named($name, $action, Helper $helper = null)
{
return ___AppHelperDefaultVars\NamedRouteHelper::named($name, $action, $helper);
}
}
if (!function_exists('named_group')) {
function named_group($prefix, \Closure $callback, Helper $helper = null) {
___AppHelperDefaultVars\NamedRouteHelper::group($prefix, $callback, $helper);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment