-
Notifications
You must be signed in to change notification settings - Fork 76
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
Make redirect url fallback configurable #190
base: master
Are you sure you want to change the base?
Changes from 6 commits
2bfbe41
e961202
92ba4e6
3b49d2c
ef72908
741ca10
ab6eb14
be1e60d
7537a58
08aef0e
068dbb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Routing\RouterInterface; | ||
|
||
/** | ||
* Theme controller. | ||
|
@@ -40,18 +41,32 @@ class ThemeController | |
*/ | ||
protected $cookieOptions; | ||
|
||
/** | ||
* @var RouterInterface | ||
*/ | ||
protected $router; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
protected $defaultRoute; | ||
|
||
/** | ||
* Theme controller construct. | ||
* | ||
* @param ActiveTheme $activeTheme active theme instance | ||
* @param array $themes Available themes | ||
* @param array|null $cookieOptions The options of the cookie we look for the theme to set | ||
* @param RouterInterface $router | ||
* @param string|null $defaultRoute | ||
*/ | ||
public function __construct(ActiveTheme $activeTheme, array $themes, array $cookieOptions = null) | ||
public function __construct(ActiveTheme $activeTheme, array $themes, array $cookieOptions = null, RouterInterface $router, $defaultRoute = null) | ||
{ | ||
$this->activeTheme = $activeTheme; | ||
$this->themes = $themes; | ||
$this->cookieOptions = $cookieOptions; | ||
$this->router = $router; | ||
$this->defaultRoute = $defaultRoute; | ||
} | ||
|
||
/** | ||
|
@@ -73,7 +88,15 @@ public function switchAction(Request $request) | |
|
||
$this->activeTheme->setName($theme); | ||
|
||
$url = $request->headers->get('Referer', '/'); | ||
|
||
if ($this->defaultRoute) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. once the router property is nullable, you need to check if its set here as well |
||
$redirect = $this->router->generate($this->defaultRoute); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because we cannot use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point of this change is not to use a hardcoded route back to "/", because in some cases Symfony may not run in root domain. In a later commit I added "/" as a fallback in case there is no route set in config, so we should have that covered. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default configuration for route is now "null". When there is a route given in the configuration file, the controller uses the router to redirect back to that route. If no route is given in configuration, it defaults to "null" and the redirect goes to "/" as before. :) |
||
} else { | ||
$redirect = '/'; | ||
} | ||
|
||
$url = $request->headers->get('Referer', $redirect); | ||
|
||
$response = new RedirectResponse($url); | ||
|
||
if (!empty($this->cookieOptions)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ public function getConfigTreeBuilder() | |
->prototype('scalar')->end() | ||
->end() | ||
->scalarNode('active_theme')->defaultNull()->end() | ||
->scalarNode('redirect_fallback')->defaultValue('homepage')->end() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, fair point, I will change that to use null as default as prepared in ThemeController.php already. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
->arrayNode('path_patterns') | ||
->addDefaultsIfNotSet() | ||
->children() | ||
|
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.
can you also make the
$router
parameter nullable to maintain BC?