Setting index route in Symfony 2 - symfony

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

Related

How to set host for routes in sonata admin?

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.

Custom views (or routes) in FOSRestController

I am using FOSRest Bundle to implement Rest API's.
I have the function getTermsAction() which returns all the terms from the DB.
I have a template listTerms.html.twig which shows the list of terms.
I want to implement another function listTermsAction() which will render this template and return it.
Something like this (I added this to my controller)
/**
* #Route("/listTerms",name="listTerms")
*/
public function listTermsAction()
{
$view = $this->view(null,200)
->setTemplate("TermsBundle:Default:listTerms.html.twig");
return $this->handleView($view);
}
I tried to this by adding the following code in routing.yml file
list_terms:
pattern: /terms/listTerms
defaults: {_controller:TermsBundle:Terms:listTermsAction}
But it doesn't work!
It just shows me "null" in the browser
The Solution i have given works but that snippet should be above the rest route in my routing.yml
That is routing.yml should be like
list_term:
pattern: /terms/listTerms
defaults: {_controller:TermsBundle:Terms:listTerms}
terms:
resource: Madhuri\TermsBundle\Controller\TermsController
type: rest
prefix: /
list_term route should be above terms route

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?

Get routing requirement variable in Symfony2

in order to create a navigation for me webinterface I'd like to get a variable from the routing config of my bundle. I define the available pages in mybundle/Resources/config/routing.yml.
mybundle_homepage:
pattern: /{_locale}/{branch}/{page}
defaults: { _controller: mybundle:mycontroller:index, _locale: de, branch: x.x.x, page: start }
requirements:
_locale: de|en
page: start|functions|events|constants|styleguide
Now I had a look at the Symfony2 YAML Parser and I have to provide a filepath to it's static method parse: http://symfony.com/doc/2.0/reference/YAML.html
mycontroller.php
use Symfony\Component\Yaml\Yaml;
class mycontroller extends Controller
{
public function indexAction($_locale, $branch, $page)
{
$routing = Yaml::parse('../Resources/config/routing.yml');
var_dump($routing);
}
}
I thought I could do it that way because the folder hirarchy looks like that:
mybundle
Controller
mycontroller.php
Rescources
config
routing.yml
But it's not working. Any ideas or maybe another way to get the requirements.page array from the routing file?
Regards, Ben
You should be able to access the router service inside a class that's DI container aware. So, you can write something like:
$routes = $this->container->get('router')->getRouteCollection();
$route = $routes->get('my_route_name');
print_r($route->getRequirements());

Resources