New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Url generator #480
[RFC] Url generator #480
Conversation
Looks great but can you explain the behavior in 5th example? Why is |
{ | ||
$hasAutoItem = false; | ||
$autoItem = (isset($GLOBALS['TL_AUTO_ITEM']) && is_array($GLOBALS['TL_AUTO_ITEM'])) ? $GLOBALS['TL_AUTO_ITEM'] : []; | ||
$autoItem = array_key_exists('auto_item', $parameters) ? [$parameters['auto_item']] : $autoItem; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems weird. Is it
if (array_key_exists('auto_item', $parameters)) {
$autoItem = [$parameters['auto_item']];
} elseif (isset($GLOBALS['TL_AUTO_ITEM']) && is_array($GLOBALS['TL_AUTO_ITEM'])) {
$autoItem = $GLOBALS['TL_AUTO_ITEM'];
} else {
$autoItem = [];
}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would result in the same, yeah
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improved in 1ad5a83
As for your first TODO, we must not only handle host and scheme but all four reference types: interface UrlGeneratorInterface extends RequestContextAwareInterface
{
/**
* Generates an absolute URL, e.g. "http://example.com/dir/file".
*/
const ABSOLUTE_URL = 0;
/**
* Generates an absolute path, e.g. "/dir/file".
*/
const ABSOLUTE_PATH = 1;
/**
* Generates a relative path based on the current request path, e.g. "../parent-file".
*
* @see UrlGenerator::getRelativePath()
*/
const RELATIVE_PATH = 2;
/**
* Generates a network path, e.g. "//example.com/dir/file".
* Such reference reuses the current scheme but specifies the host.
*/
const NETWORK_PATH = 3; |
Aaaah, this is heaven! Thank you for working on it! |
@qzminski regarding point 5, this is the default behavior of Symfony |
So that explained point 6 😂 |
PR updated and added a 7th example |
|
||
$strUrl = $objUrlGenerator->generate($strAlias, $arrParams); | ||
|
||
// Remove path from absolute URLs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we use the RELATIVE_PATH
constant here and provided the base path in the context? Then we would not have to use substr()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want RELATIVE_PATH
, it would return something like ../../foobar.html
(depending on your current page).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
depending on your current page
Exactly. And if we make sure that the base path is in the context, it will be relative to it. Just like we want it :)
As it is a specific Contao service anyway, can we add a helper method that accepts a |
I thought about that too, but its rather useless if the pagemodel already has a method for that 😂
|
Yeah, you're right. So we need an 8th example 😆 |
Question: Can't we (or shouldn't we) deprecate |
Not sure I get this right. You say
But the implementation shown in this PR is not replacing it, it's be used within. So is the goal to replace it in Contao 5 and now only change the inner workings of An option to add custom logic to it like now with |
Imho it's supposed to replace it. You're right it does not (yet) fire events, but all the other stuff in |
There is another problem: the router appends |
$parameters = is_array($parameters) ? $parameters : []; | ||
|
||
// Store original request context | ||
$context = $this->getContext(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we just clone the context object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I want to change the object that is references in multiple places
Not anymore 😎 |
As discussed in Mumble on May 12th, the PR can be merged. Additionally, we want to deprecate the |
Merged in 901ce97. I'll add a PR for the other changes. |
Per our discussion, the
UrlGenerator
is supposed to replaceController::generateFrontendUrl
.It allows for simple but flexible use of Contao urls (or pages actually).
$urlGenerator->generate('foo');
$urlGenerator->generate('index');
$urlGenerator->generate('foo/{items}', ['items' => 'bar']);
$urlGenerator->generate('foo/{items}', ['items' => 'bar', 'auto_item' => 'items']);
$urlGenerator->generate('foo/{items}/{article}', ['items' => 'bar', 'article' => 'test']);
$urlGenerator->generate('foo/{items}.html', ['items' => 'bar', 'article' => 'test']);
Example with
PageModel
ToDo