Symfony direct a subdomain to a route - php

I have a Symfony project hosted in a.example.com. In it, let's say a.example.com/site2 must be accessible through b.example.com. Simply put when I access b.example.com from the browser it must load a.example.com/site2 without showing a.example.com/site2 in the address bar (not redirecting). Is there a way to do this? Is this doable through htaccess?

you can use the 'host' property in your routing.yml as follow
acme_hello:
resource: "#AcmeHelloBundle/Resources/config/routing.yml"
host: "hello.example.com"
path: "your path action in the controller"
Link : http://symfony.com/doc/2.8/routing/hostname_pattern.html

Related

Symfony 2 : Web debug tool bar not showing (404 error) and Twig path function's not working

I have built a symfony 2.6 web site and I deploy it in an url like this :
https://www.example.com/abc/
This url points on the "web" directory (as the root directory).
The web site is working fine, but there is two issues :
1) Web debug tool bar is not showing because the widget is pointing on https://www.example.com/ not on
https://www.example.com/abc/ and I don't understand why ?!
2) Same thing for Twig path() function, it is also pointing on https://www.example.com/ not https://www.example.com/abc/
So do you have any idea about that ?
I Finally Got it, "abc" is not a physical folder it's just a "virtual" path defined in the virtual host of www.example.com,
so while https://www.example.com/abc/ is pointing on web folder of my symfony project, which is the root repository by default in Symfony framework, the folder "abc" doesn't exist in reality,
In fact I use to modify in this script "/public_html/vendor/symfony/symfony/src/Symfony/Component/Routing/Generator/UrlGenerator.php" this way by enforcing the "/abc" part in the dynamically
generated url :
$url = $schemeAuthority."/abc".$this->context->getBaseUrl().$url;
I think Symfony doesn't take this case into account by default.
Other thing that can cause this issue too is to verify the scheme (http or https) of your website in the routing configuration (routing.yml) of your project, example :
test:
resource: "#testBundle/Controller/"
type: annotation
prefix: /
schemes: [http]

Incorrect domain for an URL in a Twig template using command

In my Symfony2 project, I send email with a link to connect to my website, like
LOGIN
When I use this template in a controller, the link is good.
LOGIN
But if I use this template for my mail in a ContainerAwareCommand with a cron, it looks like
LOGIN
It's not good. How am I suppose to do to make it work ?
You should Configure the Request Context Globally, as described in the doc How to Generate URLs from the Console:
you can redefine the parameters it uses as default values to change
the default host (localhost) and scheme (http). You can also configure
the base path if Symfony is not running in the root directory.
Note that this does not impact URLs generated via normal web requests,
since those will override the defaults.
# app/config/parameters.yml
parameters:
router.request_context.host: example.org
router.request_context.scheme: https
router.request_context.base_url: my/path
hope this help

Symfony2 routing no match

We have a symfony2 application. Everything was successful until we tried to create a subdomain (for a different application). For our first test with the subdomain we linked the subdomain to a route in the s2 application.
After our test the application always return a 404 code for the route used in the previous test. We return back all configurations within the server and the problem keeps.
The route is "/usuario/iniciar-sesion".
Our original configuration for the routing is:
#/src/AppBundle/Resources/routing.yml
app_user:
resource: routing/user.yml
prefix: /usuario
#/src/AppBundle/Resources/routing.yml
app_login:
path: /iniciar-sesion
defaults: { _controller: AppBundle:User:login }
We execute the next commands in console to check the routing:
php console router:debug
php console router:match /usuario/iniciar-sesion
and everything looks fine.
Everything else works fine. At this moment the hotfix is changing the prefix (we called "usuarios") and the application runs successful. After it we tried return back the original prefix, but the application keeps return the 404 code.
We execute a lot of cache:clear --env=prod and manually delete cache dir. In our local enviroment everything works fine.
What else we can check?
So as you said in the comment I think you are trying to achieve this:
#/src/AppBundle/Resources/routing.yml
app_login:
host: usuario.site.com
path: /iniciar-sesion
defaults: { _controller: AppBundle:User:login }
And in the server host configuration you need to add the subdomain as a ServerAlias and also own that subdomain I think: https://www.godaddy.com/help/add-a-subdomain-that-points-to-a-server-name-19974
We resolved the problem.
We didn't consider the folders created by the server when set up the domain. We deleted them and the application responds on the original path.

Testing multi-tenancy Laravel applications using Behat / Mink and Behat Laravel Extension

I am building a multi-tenant SaaS application which I am trying to write tests for with Behat, using Mink and the Behat Laravel Extension
When you register for an account, you get your own subdomain on the site {account}.tenancy.dev
my behat.yml file looks like so:
default:
extensions:
Laracasts\Behat:
# env_path: .env.behat
Behat\MinkExtension:
default_session: laravel
base_url: http://tenancy.dev
laravel: ~
I am having problems straight off the bat as when I try to test my registration flow, I am getting a 404 error testing that the new subdomain is accessible, all of the data has been saved correctly, manually testing the process works and the subdomain routing works.
I was wondering if there was any way to do this using Behat and how I would go about setting Behat / Mink to use wildcard subdomains to test SaaS applications?
I am running the test inside the Homestead VM.
The base_url: http://tenancy.dev configuration is used to generate a fully qualified domain URL when you utilize relative path URL's in your mink steps (IE "/home").
When you want to hit a domain different from the domain specified in base_url, all you have to do is use the fully qualified domain URL in your step like "http://test.tenancy.dev/fully/qualified".
So use the base_url configuration to set what you will be using for the majority of your steps as relative url's and then explicitly specify the full domain for the exceptions.
When I create an account named foo
And GET "http://foo.tenancy.dev/ping"
Then I get a 200 response code
When I GET "/home"
Then the response contains "Sign Up"
If the majority of your testing will be against the sub domain, set that as your base_url and explicitly specify your top level domain when necessary.
You may resolve subdomains using xip.io, which is especially useful if you cannot access the /etc/hosts file on a CI server, for example.
To route {account}.tenancy.dev to your local webserver, you can use account.tenancy.dev.127.0.0.1.xip.io which resolves to 127.0.0.1.
After a short while I revisited this problem and found a rather simple solution to be used in my FeatureContext.php:
$this->setMinkParameter('base_url', $url);
This changes the base url for any scenario it is used in:
/**
* #Given I visit the url :url
*/
public function visitDomain($url)
{
$this->setMinkParameter('base_url', $url);
$this->visit('/');
}
Which is used in the following way:
Scenario: Test Multi Tenancy
Given I have a business "mttest"
When I visit the url "http://mttest.example.com"
Then I should see "mttest"
Obviously this is slightly contrived but does show that what I was intending to do is possible.

Access external directories from symfony2

I have images that are stored in a directory in /var/lib/myproject. So they are outside of my symfony project. I would like to display them in a twig template or via css.
How could I access them as symfony is catching every url and rewrites them?
Setting a specific route doesn't work :
my_project_res:
resource: "/var/lib/myproject"
prefix: /res

Categories