Created
January 16, 2019 06:22
-
-
Save darkcolonist/ab4f158088ae1aba3c515a957a572450 to your computer and use it in GitHub Desktop.
wildcard routing to controller in Laravel 5.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// taken from: https://laravel-tricks.com/tricks/wildcard-routing-setup as of January 16, 2019 11:12 AM | |
// This trick has been superseded by Laravel Sketchpad | |
// https://github.com/davestewart/laravel-sketchpad | |
// tested working for Laravel 5.7 | |
/** | |
* Wildcard routing function | |
* | |
* @param \Illuminate\Routing\Router $routeObject as the router instance | |
* when calling from \routes\web.php, usage: | |
* MyHelper::wildcard($this, 'test', 'Test\MyBaseController') | |
* | |
* @param string $route Base route, i.e. 'test' | |
* @param string $controller Path to controller, i.e. 'TestController' | |
* @param string|array $verbs Optional Route verb(s), i.e. 'get'; defaults to ['get', 'post'] | |
* @usage wildcard('test', 'TestController'); | |
* @usage wildcard('test', 'TestController', ['get']); | |
* @usage wildcard('test', 'Test\TestController'); | |
* | |
* @example http://localhost/test/hello/world | |
* @example http://localhost/test/goodbye/world | |
* @example http://localhost/test/foo/1/2/3 | |
* | |
* @author Dave Stewart | [email protected] | |
*/ | |
static function wildcard(\Illuminate\Routing\Router $routeObject, $route, $controller, $verbs = ['get', 'post']) | |
{ | |
// variables | |
$stack = $routeObject->getGroupStack(); | |
$namespace = end($stack)['namespace']; | |
$controller = $namespace . '\\' . $controller; | |
// routing | |
$routeObject->match($verbs, rtrim($route, '/') . '/{method}/{params?}', function($method, $params = null) use ($route, $controller) | |
{ | |
// method | |
$callable = $controller . '@' . $method; | |
// call | |
if($params) | |
{ | |
// variables | |
$values = explode('/', $params); | |
$ref = new \ReflectionMethod($controller, $method); | |
$params = $ref->getParameters(); | |
$args = []; | |
// map route segments to the method's parameters | |
foreach ($params as /** @var \ReflectionParameter */ $param) | |
{ | |
// parse signature [match, optional, type, name, default] | |
preg_match('/<(required|optional)> (?:([\\\\a-z\d_]+) )?(?:\\$(\w+))(?: = (\S+))?/i', (string) $param, $matches); | |
// assign untyped segments | |
if($matches[2] == null) | |
{ | |
$args[$matches[3]] = array_shift($values); | |
} | |
} | |
// append any remaining values | |
$values = array_merge($args, $values); | |
// call | |
return \App::call($callable, $values); | |
} | |
else | |
{ | |
return \App::call($callable); | |
} | |
})->where('params', '.*'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
my implementation