Override homepage controller in Sylius - symfony

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.

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" }

Locale as prefix and global for the whole site

I'm trying to define the routes of my site with {_locale} as a prefix so there will be routes like:
mysite.com/about-us
mysite.com/es/about-us
mysite.com/en/about-us
My first problem is that defining {_locale} as prefix makes it mandatory and the route mysite.com/about-us won't work. Currently defined this way:
#Acme/WebBundle/Resources/config/routing.yml
app_about_us:
path: /{_locale}/about-us
defaults: { _controller: AcmeWebBundle:Static:aboutUs, _locale: es}
requirements:
_locale: es|en
#app/config/routing.yml
Acme_web:
resource: "#AcmeWebBundle/Resources/config/routing.yml"
prefix: /
Also, I don't want my routes to display the {_locale}, so that if anyone types:
mysite.com/es/about-us it changes to *mysite.com
Finally if there is any way to set the prefix globally for all the site instead of putting it on every route would be awesome.
OK, first of all you can set default locale for a whole application
# app/config/config.yml
framework:
default_locale: en
Second - doing it this way, you can declare two routes:
app_about_us:
path: /{_locale}/about-us
defaults: { _controller: AcmeWebBundle:Static:aboutUs}
requirements:
_locale: es|en
app_about_us_locale:
path: /about-us
defaults: { _controller: AcmeWebBundle:Static:aboutUs, _locale: es}
Im not sure is it possible to do it different way without additional bundles, events etc...
Other way
You can set session locale - here is example to make locale sticky.
This way you can create normal routing, without params. So how set new session locale? Create special routing to setting new locale and redirect to your original page.
app_set_locale:
path: /{_locale}/
defaults: { _controller: AcmeWebBundle:Locale:setLocale}
inside controller set locale and redirect to previous page
Read Symfony documentation about Routing, Locale, Translate - maybe you will found something interesting :)

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?

Symfony2: Multiple pattern url in routing.yml

I'v tried solution given in this post.
But doesn't go in my case.
I'm trying the following
_mydomain_home_legacy_terms:
pattern: /{prefix}terms
defaults: { _controller: MyDomainHomeBundle:LegacyTerms:index, prefix:'' }
requirements:
prefix: |legacy/|legacy_|legacy%20
May I use annotations?

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