-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[NFR] Support default parameters in Router/URL generation. #1078
Comments
👍 |
@phalcon Do you have any objection to the behaviour that I described? We have implemented router defaults and can issue pull request. Let me know whether you'd like to review it first. Thanks. |
A friend has written a fix for this and I've tested it, seems like working fine. The problem is that he does not have more time to spend on this. Can I send you the patch for your review and possible merge? |
OK looking |
The patch looks good to me, I will try to write a test case. Is your friend OK if his patch is included to Phalcon? |
He is totally OK with it. I've been writing a sort of a test myself. Maybe you will find this useful. It does not cover all cases, however. <?php
$di = new Phalcon\DI\FactoryDefault;
$defController = 'home';
$defAction = 'index';
$router = $di['router'];
$router->removeExtraSlashes(true)
->setDefaultNamespace('My\Controllers')
->setDefaultModule('public')
->setDefaultController($defController)
->setDefaultAction($defAction);
$router->add('/:controller/:action/:params', [
'controller' => 1,
'action' => 2,
'params' => 3
])->setName('secured');
$offset = '/offset/';
$testData = [
'secured' => [
[
'params' => null,
'expected' => $offset,
'descr' => '
/**
* Info: Case when no route parameters are provided, and all default values will be used (NULL behaviour).
* Expected: Offset value only, without any default values appended
*/'
],
[
'params' => [],
'expected' => $offset,
'descr' => '
/**
* Info: Case when no route parameters are provided, and all default values will be used (empty array behaviour).
* Expected: Offset value only, without any default values appended
*/'
],
[
'params' => [
'params' => ['a', '1', 'b', 'c']
],
'expected' => $offset . $defController . '/' . $defAction . '/a/1/b/c/',
'descr' => '
/**
* Info: Case when only ":params" fragment is provided and is an array.
* Expected: Offset value appended by default controller, default action and ":params" array values imploded by "/".
*/'
],
[
'params' => [
'params' => []
],
'expected' => $offset . $defController . '/' . $defAction . '/a/1/b/c/',
'descr' => '
/**
* Info: Case when only ":params" fragment is provided and is an empty array.
* Expected: Offset value only, without any default values appended
*/'
],
],
];
foreach ($testData as $routeName => $cases) {
foreach ($cases as $idx => $case) {
$url = $di['url'];
$url->setBaseUri($offset);
$expected = $case['expected'];
$params = (array) $case['params'] + ['for' => $routeName];
$actual = $url->get($params);
echo "-------- Running case #" . ($idx + 1) . " --------\n";
if ($actual !== $expected) {
echo "Test failed. Expected value: '{$expected}', actual: '{$actual}'\n";
echo "{$case['descr']}\n\n";
} else {
echo "Passed: {$actual}\n\n";
}
}
} |
|
Are you sure about the fourth test case? /offset/home/index/ looks good to me |
reviewing it.. |
Yes, you are right. For the fourth the expected value should be What about the third case - should there be a trailing slash or not? |
I think not. All parameters are joined with a slash, thus [a, 1, b, c] => a/1/b/c |
OK the code works for me except this thing:
As far as I can tell, URL is unaware of all those routing events. This gives us
This actually makes sense to me: URL uses the default values from the router, not from the matched route. Is this OK for you? |
yes. If you get the fix in, I'll write more tests and if I have further questions, I'll bug you once more. thanks! |
git checkout -b url-defaults
git pull https://github.com/sjinks/cphalcon.git url-defaults if you want to test it. |
Are you waiting for me to test this first, before you do pull request? |
No, I am cleaning the patch up ATM. But if you can run some tests to make sure everything is good, that would be great :-) |
I will test it ASAP, when I have a small break from my work project. I will most likely come up with more improvements :-) |
@sjinks Any movement on this? Waiting impatiently :) |
@temuri416 I am sorry to say that the patch had to be reverted because it broke the existing code badly (#1938, #1961). However, @xboston said he would add more test cases to cover Mvc\Router's code; once this is done and we have better test coverage, I will return to this case. |
oh well. as a general comment, I'd like to say that URL generation in Phalcon is quite painful, especially when it comes to assembling URL when you're trying to match route's named segments. please let me know if you'd like me to formulate it further. thank you. |
I tend to agree… However, without a good test coverage it is very hard to rewrite it — because my changes will likely break backwards compatibility. If I rewrite the router and related things in PHP, will you be able to help improving the PHP code? |
I will be more than happy to help you doing that. |
Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues |
The code and comments below:
When generating URLs, the router should be able to fall back to default parameter values if they are not provided to $url->get() method.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: