Comments (17)
@fenric first, but it's not URI - it's path
from http-router.
Hi! Now I prepare the 2.0.0 version . I will try to develop your feature.
from http-router.
Great! Thanks. I could submit a pr if you want.
from http-router.
// Defines the pattern of a `<attribute>`
protected const ATTRIBUTE = '<([a-zA-Z0-9_]++)>';
// Matches a URI group and captures the contents
protected const GROUP = '\(((?:(?>[^()]+)|(?R))*)\)';
public const SECTION = '#(?:' . static::ATTRIBUTE . '|' . static::GROUP . ')#u';
/**
* @var array Stores compiled URIs
*/
protected $compiledUri = [];
/**
* Recursively compiles a path of a URI specification by replacing the specified attributes and
* any optional attributes that are needed.
*
* @param string $path Part of the URI specification
* @param bool $required Whether or not attributes are required (initially)
* @param array $attributes Specified attributes
* @return array Tuple of the compiled path and whether or not it contained specified attributes
* @throws InvalidArgumentException
*/
protected function compileUri(string $path, bool $required, array $attributes)
{
$missing = [];
$uri = preg_replace_callback(
static::SECTION,
function ($matches) use (& $missing, & $required, $attributes) {
if ($matches[0][0] == '<') {
// Unwrapped attribute
$attr = $matches[1];
if (isset($attributes[$attr])) {
// This path is required when a specified attribute does not match the default.
if (! $required) {
$required = ! isset($this->attributes[$attr]) || $attributes[$attr] !== $this->attributes[$attr];
}
// Add specified attribute to this result
return $attributes[$attr];
}
// Add default attribute to this result
if (isset($this->attributes[$attr])) {
return $this->attributes[$attr];
}
// This path is missing a attribute
$missing[] = $attr;
} else {
// Unwrapped group
$result = $this->compileUri($matches[2], false, $attributes);
if (! empty($result[1])) {
// This path is required when it contains a group that is required
$required = true;
// Add required groups to this result
return $result[0];
}
// Note: don't add optional groups to this result!
}
},
$path
);
if ($required && $missing) {
$missing = implode(', ', $missing);
$msg = sprintf('Route %s: required attributes (%s) not passed', $this->id, $missing);
throw new InvalidArgumentException($msg);
}
return [$uri, $required];
}
/**
* Generates a URI for the current route based on the attributes given.
*
* @param array $attributes URI attributes
* @return string Compiled URI
*/
public function getUri(array $attributes = [])
{
$key = md5(serialize($attributes));
if (! isset($this->compiledUri[$key])) {
if ($attributes) {
$attributes = array_map('rawurlencode', $attributes);
// Decode slashes back, see Apache docs about AllowEncodedSlashes and AcceptPathInfo
$attributes = str_replace(['%2F', '%5C'], ['/', '\\'], $attributes);
}
list($uri) = $this->compileUri($this->path, true, $attributes);
// Trim all extra slashes from the URI
$this->compiledUri[$key] = preg_replace('#//+#u', '/', $uri);
}
return $this->compiledUri[$key];
}
from http-router.
chubbyphp/chubbyphp-framework#6
from http-router.
what do you think is better?
$router->getRoute('read.entity')->getUri(['id' => 100]);
or
$router->getUri('read.entity', ['id' => 100]);
from http-router.
It's will be added in the 2.0 version.
from http-router.
$router->getPath('read.entity', ['id' => 100]);
from http-router.
@dominikzogg 1. compileRoutePath/generateRoutePath pure for users 2. this not SOLID
from http-router.
@fenric first, but it's not URI - it's path
yes, you're right, but... method Route::getPath()
already exists... maybe Route::buildPath()
?
$router->getRoute('read.entity')->buildPath($attrs = ['id' => 100], $strict = true);
what do you think?
from http-router.
@fenric it's not SOLID. in my case current route in request object.
from http-router.
it's not SOLID. in my case current route in request object.
but...
$route->buildPath($attrs = ['id' => 100], $strict = true);
or for your case...
$this->getRequest()->getRoute()->buildPath($attrs = ['id' => 100], $strict = true);
it's also correct...
$router->getRoute('read.entity')->buildPath($attrs = ['id' => 100], $strict = true);
from http-router.
@fenric my error, incorrectly read your comment
from http-router.
the version 2.0.0 contains it feature.
examples:
$route->buildPath(array $attributes, bool $strict);
or...
$router->getRoute('foo')->buildPath(array $attributes, bool $strict);
from http-router.
The official release of the new version will be released at the coming days.
from http-router.
it seems that buildPath does not support /path(/{optional)}
from http-router.
@dominikzogg look https://github.com/sunrise-php/http-router/blob/release/v2.0.0/functions/path_parse.php - it's too hard for me :) I'm write other version mixing my old router with that. When all done, we will compare and maybe combine versions.
from http-router.
Related Issues (20)
- ServerRequestFactory helper without inherit it as an argument
- Too few arguments to function Cosmic\App\Controllers\Home\Index::index(), 1 passed HOT 2
- How to response Json?
- Containers
- User experience improvements (v3)
- chubbyphp-framework released with http-router 2 support
- Don't create the QueueableRequestHandler if the router/route doesn't contain middleware
- MethodNotAllowed is thrown when not needed and vice versa HOT 2
- PHP 8 support HOT 2
- PHP8 attributes support HOT 1
- Creating custom named attributes for routes
- Flexible request handler definition for route HOT 5
- Create a command for the list of routes
- Create the router builder
- Create a new Middleware annotation for route declaration in one line HOT 1
- Serialization of ReflectionClass is not allowed
- Dependency Dashboard
- Short version of URI generation
- Provide the changelog with the next version
- Gets a route holder HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from http-router.