Divide symfony app for different domains - php

I would know, does it is possible to divide Symfony 4 application for 2 domains. I would see it as :
routes.yaml
index:
path: domain1.com/
controller: App\Controller\ServiceController::indexAction
results:
path: domain1.com/results
controller: App\Controller\ServiceController::resultsAction
admin:
path: domain2.com/admin
controller: App\Controller\AdminController::indexAction
I would limit routes for domain1 and others available only from domain2.
I know I can just part my app for 2 new, I can try with apache hosts config, but I think easiest and fastest is my idea. Ah. I don't speak about subdomains.

You can add host to your route, you can read more here

Related

Symfony direct a subdomain to a route

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

Make Symfony2 resources work with SSL

I purchased an SSL certificate at OVH in order to have a URL https. They set me the certificate on my website but now, when I access using the https://www.shootandgo.fr , I get errors like
:net::ERR_SSL_UNRECOGNIZED_NAME_ALERT
and the images, the CSS files, and everything else are not found...
I use Symfony 2 and all my resources are on local, in the web directory of Symfony.
OVH has said "we need to tell Symfony2 to use HTTPS" but I do not see how... does anyone have a solution? Thank you in advance!
PS: Sorry for my English... I'm French ^^'
To make symfony2 work with HTTPS, you need to reference these sections in their manual:
http://symfony.com/doc/current/cookbook/routing/scheme.html
http://symfony.com/doc/current/cookbook/security/force_https.html
More information could be found by the links above, but generally speaking everything is defined in app configs:
secure:
path: /secure
defaults: { _controller: AppBundle:Main:secure }
schemes: [https]
and this:
# app/config/security.yml
security:
# ...
access_control:
- { path: ^/secure, roles: ROLE_ADMIN, requires_channel: https }
The said above was related to Symfony2 thing, which you was asking about.
But my guess is, that this error ERR_SSL_UNRECOGNIZED_NAME_ALERT belongs to the wrong server/certificate setup, not to specific framework you are using. You'd need to elaborate more, and to provide additional information to figure this out.
Well i have upload again my website on the server, and everything is normal now...I don't know why there was a problem on it las tt

Set Access control in routing file like #Secure annotation above controller action

I have the following ACL settings in my secirity.yml file
fos_user_resetting:
path: /backend/request
roles: [IS_AUTHENTICATED_ANONYMOUSLY]
super_admin_pages:
path: /backend/.*
roles: [ROLE_SUPER,ROLE_USERS]
Also i found setting access permission using #secirity tag in the routing annotation.
My question is, is it possible to add security (access permission according to roles like in secirity.yml) for each path in routing.yml file
Nope, there is no such argument for securing routes directly in your routing file. The common part between ACL and routing are paths.
See all the details in the Security chapter of Symfony doc.

Symfony2 routing with two bundles issue

I have issue with routing in my Symfony 2 application.
This application contains 2 bundles MainSiteBundle and GalleryBundle
I configured routing with prefixes like that:
app/config/routing.yml
honorata_photo_main_site:
resource: "#HonorataPhotoMainSiteBundle/Resources/config/routing.yml"
prefix: /
honorata_photo_gallery:
resource: "#HonorataPhotoGalleryBundle/Resources/config/routing.yml"
prefix: /gallery
Routine inside each bundle is not important now because i have issue with this.
When I try to access / route everything works fine (even with sub routes inside bundle)
When I try to access /gallery route it shows me error like:
No route found for "GET ery"
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException ยป
Why Symfony 2 router cuts first 3 letters after / ?
I would guess that inside "#HonorataPhotoMainSiteBundle/Resources/config/routing.yml" you have some more general route defined.
Try reversing the order of definition:
honorata_photo_gallery:
resource: "#HonorataPhotoGalleryBundle/Resources/config/routing.yml"
prefix: /gallery
honorata_photo_main_site:
resource: "#HonorataPhotoMainSiteBundle/Resources/config/routing.yml"
prefix: /
Subject resolved and closed !
Problem was that /gallery path already exist in MainSiteBundle I realised that using php app/console route:debug and after deeper analysis :)
Next time I will make routing log to prevent that in the future.

How to create the route in symfony 2 which maps to external URL?

Referring to this,
http://symfony.com/doc/current/book/routing.html
we can map url pattern to controller and action
app/config/routing.yml
blog_show:
path: /blog/{slug}
defaults: { _controller: AcmeBlogBundle:Blog:show }
I want to map the path to external url.
app/config/routing.yml
blog_show:
path: /blog/{slug}
defaults: "www.example.com/blog"
The requirement is, my current website is in kohana, I am porting it gradually to symfony 2. For my symfony2 app kohana URL are like external urls, I want to configure these urls in routing and use them in standard way,
e.g. in Twig,
<a href="{{ path('blog_show'}}">
Read this blog post.
</a>
So later on when I port my pages to Symfony, I will have to change only routing file so that I could use same blog_show key to refer to url and I wont' have to change all the files where I have used urls.
You can do this by using one of the Symfony framework controllers although I'm not sure how this would work with parameters:
blog_show:
path: /blog/{slug}
defaults:
_controller: FrameworkBundle:Redirect:urlRedirect
path: "http://example.com/blog"
permanent: true
Note that path: /blog/{slug} grabs the slug directly, but path: "http://example.com/blog/{slug}" doesn't work.
Source: http://symfony.com/doc/current/cookbook/routing/redirect_in_config.html
As of Symfony 2.2 this is possible by adding the host constraint to the routes:
routing.yml
user_homepage:
path: /path/to/whatever
host: "sub.domain.ext"
defaults:
_controller: forExampleAnyNamespaceBundle:Controller:action
There's an official blog post on this issue: http://symfony.com/blog/new-in-symfony-2-2-url-host-support-in-the-routing
The router feature of Symfony doesn't work that way...
I suggest you create a Twig extension for this. Read more about this here:
http://symfony.com/doc/current/cookbook/templating/twig_extension.html
You could create a function that works very similar to the regular url() function, so you can migrate as easily as possible.
{{ legacyUrl('blog_post', {slug: 'my-blog-post'}) }}
After you migrated the blog to Symfony, all you need to do is create a route called "blog_post" and change "legacyUrl" to "url".
Seems there is no native Symfony way to handle this problem.

Categories