How can I support multiple dynamic hosts within Symfony Routing? - php

In my Symfony app (Symfony 5.3) I have to support the following scenario with multiple hosts/domains that belong to multiple app contexts (i.e. own firewall and own controllers, sometimes also shared controllers):
main-domain.tld -> main_context
main-domain2.tld -> main_context
service.main-domain.tld -> service_context
service.main-domain2.tld -> service_context
service.maybe-several-other-brand-domains.tld -> service_context
admin.main-domain.tld -> admin_context
admin.main-domain2.tld -> admin_context
admin.maybe-several-other-brand-domains.tld -> admin_context
How it started
Before we had multiple brands/domains, we had two main app contexts, that are addressed by their own hostnames. So we did something like this to assign the controllers to the context:
#[Route(
path: '/',
requirements: ['domain' => '%app.public_hostname_context1%'],
defaults: ['domain' => '%app.public_hostname_context1%'],
host: '{domain}',
)]
# where app.public_hostname_context1 is a hostname configured in the .env.local
How it is going
This worked well, until we decided to have more than one valid host for one of the contexts, in fact as much as the branding needs. So I did some research and came across the problem, that I cannot access the current hostname inside the defaults config and thus would have to set the domain explicitly on every url I generate.
Question is
How would you solve that requirement?

I post my first approach of a solution as a direct answer, so please discuss it or shine with a better one. Maybe, I have overseen something and I have a slight feeling that that solution may be not the best one. And for others stumbling upon the same requirement, this whole Question will document at least one solution approach. :)
First, remove the defaults from the route definitions and provide a pattern for several valid domains of a context:
#[Route(
path: '/',
requirements: ['domain' => '%app.public_hostnames_context1_pattern%'],
host: '{domain}',
)]
# app.public_hostname_context1_pattern is a pattern configured in the .env.local
# containing all possible hostnames for that context like
# PUBLIC_HOSTNAME_CONTEXT1_PATTERN=(?:service\.main-domain\.tld|service\.main-domain2\.tld)
To set the current hostname as a default for the domain parameter for all routes, I have a RequestListener inspired by this answer from 2012 that sets it, before the RouterListener does its work.
In my services.yaml:
# must be called before the RouterListener (with priority 32) to load the domain
App\EventListener\RequestListener:
tags:
- { name: kernel.event_listener, event: kernel.request, priority: 33 }
And the RequestListener:
<?php
declare(strict_types=1);
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\RouterInterface;
class RequestListener
{
public function __construct(
private RouterInterface $router,
){}
public function onKernelRequest(RequestEvent $event)
{
if (false === $this->router->getContext()->hasParameter('domain')) {
$this->router->getContext()->setParameter('domain', $event->getRequest()->getHost());
}
}
}
The good part of this is, that I can still override the domain parameter when I create URLs. But a drawback I see is: When I generate a URL for another context and don't set the domain explicitly, an error will be raised, because now the host of the current request is used as the domain for the other context and that is not allowed by the pattern within the requirements. I can live with that. Do you?

Related

Behat - Organize behat.yml file (separate authenticated contexts from others)

I am testing my PHP REST API. Some methods require the user to be authenticated, some do not. How can I separate my differents contexts in behat.yml to separate my two needs? In addition, for the moment for each method where the user needs to be authenticated I use the following type of feature:
Feature: Get things
#login
Scenario: I want to get a list of things
When I request a list of things from "/myendpoint"
Then The result should include a thing with id "myID"
where #login is a BeforeScenario method. Is this a good way to go?
Finally, I would like to store my base url somewhere so that it is accessible by all my contexts. My behat.yml looks like this for the moment:
default:
suites:
default:
paths:
features: 'features'
bootstrap: 'features/bootstrap'
contexts:
- MyFirstContext:
- 'http://localhost:8080'
- MySecondContext:
- 'http://localhost:8080'
- MyThirdContext:
- 'http://localhost:8080'
I access http://localhost:8080 in each context thanks to the context constructor:
public function __construct($baseUrl)
{
$this->base_url = $baseUrl;
}
I want to store http://localhost:8080 so that I don't have to rewrite it for each new context in my behat.yml. And if it is possible to store it in behat.yml, how can I access it in my context.php?

Symfony, dynamic routing

I have a symfony project with multiple skins/templates that have their own routes, does anyone have an idea for a correct setup?
Every skin/template is its own bundle, since its not just skins and assets, but maybe also services that might exist in some skins.
Hostname decides the skin.
Using a custom RouteLoader to load the route.yml of the target bundle.
The custom RouteLoader does the job--but the generated routes are getting cached, and as far as i understand, there is no way to prevent route caching.
Some suggestions are:
Creating a /{dynamic} route, so manually form routes.. But i dont want to throw away that piece of functionality of the router, or refactor the entire project..
Prefix the routes with the template identifier. This would require me to load all route.yml files, which isnt possible since their share paths.
Anyone? I cant go with multiple projects really, the amount of skins will be around 20-30~.
The reason for this setup is because its a target of Content-as-a-Service .. service, multiple clients use the project as a platform, and their setting decides which templates gets used.
It sounds like you want to dynamically load bundles based on the host name? Not going to happen with Symfony 2 because of the caching. Especially the services.
Your best bet is to setup an app for each skin and then do some url majic to execute the desired app.php file. Clearly since you have defined a bundle for each skin then there is a finite number so having multiple apps should not be much or a burden.
It's possible that you might be able to work around the template issue. You would still need to load all your skin bundles but you could futz around with the template names or paths and probably get something to work.
But services? Unless you start appending host names to service id's then I don't see any work around.
I think it's possible to load dynamically twig templates depending of your user by adding a listener on kernel requests.
I can give you a piece of code which, I hope, could help you :
/**
* On Kernel Request triggers the request to get the user config
* then adds TWIG paths depending on user TemplateName
*/
public function onKernelRequest(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
//$userConfig = Retrieve your user config
if (null === $userConfig->getTemplateConfig()->getTemplate()->getName())
{
throw new TemplateConfigNotFoundException(sprintf("Could not find TemplateConfig for %s", $userConfig->getName()));
}
$template = $userConfig->getTemplateConfig()->getTemplate()->getName();
$path = sprintf('%s/../../%s/Resources/views', __DIR__, ucfirst($template));
if (!is_dir($path)) {
throw new TemplateNotFoundException(sprintf("Could not find template %s", $template));
}
$this->loader->prependPath($path);
$this->loader->addPath(sprintf('%s/../Resources/views/Default', __DIR__));
}
With $this->loader defined as \Twig_Loader_Filesystem in your Listener constructor
Hope it can give you a clue
Symfony2 already supports host aware routing out-of-the-box, like this:
website_customer_1:
path: /
host: customer1.example.com
defaults: { _controller: Customer1Bundle:Main:startPage, theme: template1 }
website_customer_2:
path: /
host: customer2.example.com
defaults: { _controller: Customer1Bundle:Main:startPage, theme: template2 }

Symfony 2 dynamic routing (e.g. for stores)

I'm new at Symfony 2, now I'm trying to get a dynamic routing, I mean really dynamic.
For example:
example.com/en/categoryLevel1/categoryLevel2/categoryLevel3/productId-ProductName
OR
example.com/en/categoryLevel1/categoryLevel2/productId-ProductName
OR
example.com/en/categoryLevel1/categoryLevel2/categoryLevel3/
The number of category levels (the category depth) have to be flexible to 100%. It must be possible and able to use one level to twenty levels.
Where is the entry point to setup this (which classes are doing those routing stuff)?
Another example is:
routes on the old page:
example.com/{categoryLvl1}/{categoryLvl2}/.../p-{productId}
at the new page are some changes in the routes:
example.com/{lang}/{catLevel1}/{catLevel2}/.../{productId}-{productName}
how i do the regex, etc.. i know. But i can't find the routing process in symfony (better the pre-routing process). I would like to build an pre-routing class and fallback the "normal" symfony2 routing. i have to match old and new, both are completely dynamic.. the old one is written in ZF1 (pretty easy for me) but symfony2 is a new area for me...
Let's assume you have a bundle that handles this type of URL, you might add the following in the bundle's routing.yml (I prefer yml, YMMV).
YourSomethingBundle_main_any:
pattern: /{request}
defaults: { _controller: YourSomethingBundle:Main:dispatcher }
requirements:
request: ".*"
Important: This is a “catch-all”, letting you process the actual request path in your controller. You should either prefix the pattern path, or load this bundle after all other bundles, or other routes will no longer work.
As per SF2 conventions, you would now have a MainController class with a dispatcherAction method:
<?php
namespace Your\SomethingBundle\Controller;
use \Symfony\Bundle\FrameworkBundle\Controller\Controller;
class MainController extends Controller
{
public function dispatcherAction($request='')
{
$request = preg_split('|/+|', trim($request, '/'));
// ... and so on.
}
}

How do I change the URL Alias for Security/login in SilverStripe to user/login

I am working on a new website being built in SilverStripe. Currently I am having a ton of trouble trying to get the system to let me change the URL alias (or create a second one) for the Security controller's login (and eventually logout) function.
I have tried playing around with the routes.yml file and I tried creating the paths in my own UserController and loading directly from the Security controller with "return Security::login()". But that gives me errors about the use of the static functions.
Unfortunately I don't come from a ton of object oriented experience and this is the first CMS I have used that actually uses a bunch of true object orientation. The current version of SilverStripe we are using is 3.0 (but we will be upgrading to 3.1.1 in a few days).
Does anyone know much about the routing in SilverStripe?
as you stated correctly, SilverStripe has routes, and they can be defined in a yaml config file.
if you take a look at the existing routes.yml in the framework you can see how the default security route is defined:
https://github.com/silverstripe/silverstripe-framework/blob/fd6a1619cb7696d0f7e3ab344bc5ac7d9f6cfe77/_config/routes.yml#L17
if you just want to replace the Secuirty in Secuirty/login, its as easy as just creating your own routes.ymlin mysite/_config/ with the following content:
---
Name: myroutesorsomeotherrandomname
Before: '*'
After:
- '#rootroutes'
- '#coreroutes'
- '#modelascontrollerroutes'
- '#adminroutes'
---
Director:
rules:
'foobar//$Action/$ID/$OtherID': 'Security'
NOTE: make sure you ran a ?flush=1 to ensure the yml file is loaded (they get cached)
also make sure you use spaces in the yml file, if you use tabs you are going to have a bad time
if you however wish to also replace /login and /logout this is no longer a thing for routing.
login and logout are actions (php functions that are listed in Security::$allowed_actions and thus available as URL) on the on Security.
but its still rather easy, just subclass Security and create the actions you want:
<?php
class MySuperSecurity extends Security {
private static $allowed_actions = array(
'showMeTheLoginForm',
'alternative_login_action_with_dashes',
'doTheLogout',
);
function showMeTheLoginForm() {
// can be visited via http://website.com/foobar/showMeTheLoginForm
return $this->login();
}
function alternative_login_action_with_dashes() {
// can be visited via http://website.com/foobar/alternative-login-action-with-dashes
return $this->login();
}
function doTheLogout($redirect = true) {
// can be visited via http://website.com/foobar/doTheLogout
return $this->logout($redirect);
}
}
and make the route point to your new class instead of Security inside the routes.yml:
'foobar//$Action/$ID/$OtherID': 'MySuperSecurity'
NOTE: again, make sure you did a ?flush=1, both the private static $allowed_actions as well as the yml config file are cached by silverstripe.
NOTE: both solutions suggested in this post will create an additional route to login and does not replace the existing one, so the old Security/login will still be available
I don't know nothing about SilverStripe excepting that is a CMS, but i think SilverStripe must provide a way to aliases Url. Also an alternative is create Alias in virtual host definition if you're using apache or in .htaccess file. Refer to apache doc to further details. If you post a skeleton of your .htaccess file or VirtualHost definition i could help you.

Removing Symfony2 scope widening notice

I'm trying to speed up my stack by removing references to the service container where possible. In this case I only need the request:
email_error_message:
class: Core\MyBundle\Services\Email\ErrorMessage
arguments: [ #request, %params ]
However, that throws a scope widening issue. I'm not concerned with refactoring the code for now, I just wish to get rid of the warning by adding strict = true. But I can't seem to get the YAML syntax right:
email_error_message:
class: Core\MyBundle\Services\Email\ErrorMessage
arguments:
- { type: service, id: request, strict: false }
- %params%
This isn't working though. Any ideas?
EDIT
I realise I could change the scope of this service to request, but that isn't an option in this case.
You want to restrict the scope of the service to the request scope, since you need to make sure you're passed the right Request instance - if you are using the service from within a subrequest for example, or whether through the main request. Adjust your service config to:
services:
email_error_message:
class: Core\MyBundle\Services\Email\ErrorMessage
scope: request
arguments: [ #request, %params% ]
See the docs for more details.
Edit as per your question edit, you're not able to change the scope. In which case, your syntax should be as follows:
services:
email_error_message:
class: Core\MyBundle\Services\Email\ErrorMessage
arguments: [ #request=, %params% ]
with the appended = symbol. Note that I've not seen this referenced anywhere, and it's from digging around in the code for the DI container ;-)

Categories