How to set host for routes in sonata admin? - symfony

How to make sonata always use root domain for url it generates?
I have a site, some of the pages are on the root domain, some are on the subdomain.
I need to have url to edit post that always refers to root domain even if the link is on the subdomain.
Edit

It looks like I could solve the issue for sonata
adding to sonata route options and defaults in routing.yml
admin_area:
resource: "#SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /backoffice
options:
compiler_class: 'MyApp\Router\RouteCompiler'
defaults:
default_host: %router.request_context.host%
_sonata_admin:
resource: .
type: sonata_admin
prefix: /backoffice
options:
compiler_class: 'MyApp\Router\RouteCompiler'
defaults:
default_host: %router.request_context.host%
router.request_context.host is my root domain.
The route compiler class looks like:
class RouteCompiler extends BaseRouteCompiler
{
public static function compile(Route $route)
{
if ($route->getHost() === "") {
$route->setHost($route->getDefaults()['default_host']);
}
return parent::compile($route);
}
}
When I use in twig url function, it generates url to root domain even on
pages from subdomain.

Related

URL based on locale

Is it possible to change URL when locale is changed?
This is my route:
contact:
path: /{type}
defaults: { _controller: WebPortalBundle:Default:contact }
requirements:
type: kontakty|contact
Is it possible when locale is "en" to display url with type = contact, when russian, czech, slovak, display with type = kontaky ?
Yes, it's possible. We are using https://github.com/BeSimple/BeSimpleI18nRoutingBundle that bundle for doing that.
After installation bundle you need to open config.yml file and add this config.
be_simple_i18n_routing: ~
After that open your main routing.yml file(app/routing.yml) and simply add type "be_simple_i18n". It's shoud be like that.
acme:
resource: "#AcmeBundle/Resources/config/routing.yml"
prefix: /
type: be_simple_i18n
And finally open bundle spesific routing.yml file (in that case AcmeBundle/Resources/config/routing.yml).
acme_contact:
path: /
defaults: { _controller: AcmeBundle:Default:contact }
locales: { en: "/contact", ru: "/kontaky" }

Override homepage controller in Sylius

I am using http://docs.sylius.org/en/latest/bundles/general/overriding_controllers.html as a reference when trying to override the homepage controller.
I tried the following in config.yml:
sylius_web:
driver: doctrine/orm
classes:
frontend:
homepage:
controller: SpaceDice\WebBundle\Controller\Frontend\HomepageController
And it doesn't seem to be working.
What would I put in config.yml to specify the controller from the homepage?
In: vendor/sylius/src/Sylius/Bundle/WebBundle/Resources/config/routing/main.yml I see:
sylius_homepage:
path: /
defaults: { _controller: sylius.controller.frontend.homepage:mainAction }
but I'm not sure how to override/set sylius.controller.frontend.homepage.
Thanks
I was able to figure out a couple ways to do this.
First, you can set the parameter in config.yml:
parameters:
sylius.controller.frontend.homepage.class: My\Controller\Here
Another option is to set the homepage route of / in my AppBundle/Resources/config/routing.yml
homepage:
path: /
defaults: { _controller: AppBundle:Frontend\Homepage:main }
Ensure that your routing config is processed before the Sylius WebBundle config (listed first in app/config/routing.yml) and you can take it from there.

Symfony2 - Parent routes read instead

I am working on Symfony2 project in which a kind of general bundle, let say CoreBundle, is managing all the routes (in this form, first.domain/a-route, second.domain/a-route, third.domain/a-route,...) of the website. Now I have been creating FirstBundle, SecondBundle, ThirdBundle with the idea to "transfer" the management of routes of each subdomain (firt, second, third,...) to the related bundle.
Beginning with the transfer of routes from CoreBundle to FirstBundle by editing /app/config/routing.yml file from:
resource: "#ProjectFirstBundle/Resources/config/routing.yml"
prefix: /
host: "{subdomain}.{domain}"
defaults: { _controller: ProjectFirstBundle:Public:aroute }
domain: %project_domain%
requirements:
domain: "%project_domain%"
subdomain: 'first'
to:
project_first_aroute:
path: /a-route
host: "{subdomain}.{domain}"
defaults: { _controller: ProjectFirstBundle:Public:aroute, domain: "%project_domain%" }
requirements:
domain: "%project_domain%"
subdomain: 'first'
And of course I have created controller and view files using the same schema as CoreBundle (by making an adaptation -- inheritance for .twig files).
Now the problem is only parent route (that is CoreBunble /a-route route) is being read when running the URL first.domain/a-route.
Any suggestions?

cant run the controller with symfony 2

I generated my bundle with netbeans with no error , then went to my main config file in the app directory and set this:
first_app:
resource: "#FirstAppBundle/Resources/config/routing.yml"
prefix: /
Then set this in my bundle routing.yml file:
first_app_homepage:
pattern: /hello/{name}
defaults: { _controller: FirstAppBundle:Hello:index }
I have got this controller:
namespace DimaVendor\MytestsBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController extends Controller {
public function indexAction($name) {
return new Response('<html><body>' . $name . '</body></html>');
}
}
I get 404 when I go to there:
http://localhost/app.php/hello/Ryan
why?
I also cleaned my cache and found no error
app.php and app_dev.php scripts are the entry points for Symfony apps, and they lie in web/ subfolder in the project - in this case, point your browser to:
http://localhost/PhpProject1/web/app_dev.php/hello/Ryan
.htaccess comes in web/ folder, and redirects any requests to:
http://localhost/PhpProject1/web/some/path
to:
http://localhost/PhpProject1/web/app.php/some/path
in your case. Have a look!

Setting index route in Symfony 2

I've got Symfony2 installed, and a mostly working site, the only issue is I don't know how to set a default route. Currently, I access my index and other routes with the following URLs:
www.example.com/app_dev.php/index
www.example.com/app_dev.php/example_route
I'd like to have www.example.com default to the index route, so I can get the same results with the following URLs:
www.example.com
www.example.com/example_route
I'm running lighttpd as my web server. How can I configure lighttpd/Symfony2 to do this?
Just create a route that maps to the / pattern :
# app/config/routing.yml
homepage:
pattern: /
defaults: { _controller: AcmeHomeBundle:home:show }
This will route to whatever controller you specify.
Great Docs:
How to Configure a Redirect without a custom Controller
Routing
I used below code to set home page route.It's working fine
Symfony version : Symfony 3.2.8
homepage:
path: /
defaults: { _controller: AppBundle:Home:index}
For me Symfony 4.1.x
Edit the file
# app/config/routes.yaml
index:
path: /
controller: App\Controller\YourIndexController::yourIndexFunction
There's
App\Controller is the namespace you declare at the start of the Controller class, and after is your class name and method name to route to.
I solved this problem by just removing the following from routing_dev.yml
_welcome:
pattern: /
defaults: { _controller: AcmeDemoBundle:Welcome:index }
That is assuming you have setup a default / route in a routing.yml file or by defining the route in a controller like:
/**
* #Route("/")
* #Template()
*/
public function indexAction()
{
return array('name' => '1');
}
The answer given by ManseUK is very much helpful but I have little elaboration
1)
# app/config/routing.yml
homepage:
pattern: /
defaults: { _controller: AcmeHomeBundle:home:show }
2) rename the app_dev.php to index.php and this will route to the home page automatically

Resources