Symfony 2 route with no custom controller code - php

I have my routes defined using annotations in my SF2 application, however there are a handful of pages which although they have a distinct route and Twig template they require no controller code whatsoever which leads to empty methods such as this:
/**
* #Route(
* "/courselimit",
* name = "course_limit"
* )
* #Template("CRMPiccoBundle:Course:Limit.html.twig")
*
* #param Request $request
*
* #throws \Exception
*/
public function courseLimitAction(Request $request)
{
}
This, to me, seems pointless and messy. Is there a way to avoid this in SF2 without converting all my routes to be managed in YAML files?

You would edit app/config/routing.yml like so:
# app/config/routing.yml
course_limit:
path: /courselimit
defaults:
_controller: FrameworkBundle:Template:template
template: path/Limit.html.twig
Examples are shown in the Render Template without a custom Controller:
http://symfony.com/doc/2.7/templating/render_without_controller.html

Related

Symfony 3 - Route excluding beginning of specific path (url)

I'm creating an application with Symfony 3.2.9, and I would to do a Admin panel to manage application. Application works like CMS, so is creating new pages with URL, like domain.com/pagename1 and also domain.com/pagename1/subpagelevel2 ect. Problem is when I want to create URL for Admin panel, and URL should looks like: domain.com/admin, but also admin panel need some sub pages, like domain.com/admin/manage or domain.com/admin/manage/edit/1 ect.
I created DefaultController with routing like :
/**
* #Route("/", name="homepage")
*/
and AdminController with routing like :
/**
* #Route("/admin", name="admin")
*/
Problem is that when I want to dynamically create new sub page of application I need to create routing like:
/**
* #Route("/{page}")
*/
But this overwrite my Admin panel sub pages (eg. domain.com/admin/manage).
Is it way, to exclude or overwrite path from default DefaultController by AdminController? I want to have possibility to create all URL-s from DefaultController excepts paths beginning like domain.com/admin ... and so on.
From documention in https://symfony.com/doc/current/routing.html you can use the requirements parameter to specify a more strict match
I guess something like this whould work:
DefaultController:
/**
* #Route("/", name="homepage")
*/
AdminController:
/**
* #Route("/admin", name="admin")
*/
Other Controller:
/**
* we exclude page=admin from this controller
* #Route("/{page}", requirements={"page": "^(?!admin).+"}))
*/
Routes are searched in the order they are listed - so put the most generic at the end of the list, and it will find and use /admin before /{page}
For example, one of my last routes at the bottom of app/conf/routing.yml is
# http://symfony.com/doc/current/routing/redirect_trailing_slash.html
remove_trailing_slash:
path: /{url}
defaults:
_controller: AppBundle:Default:removeTrailingSlash
requirements:
url: .*/$
methods: [GET]
Cleanest on your use case:
Why not simply create a separate Bundle for Admin and put a prefix in AdminBundle routes?
Depend on routing orders and/or REGEX in routes is not recommended just to avoid a Bundle Creation. This is for what Bundles have been thought.
app/config/routing.yml
admin:
resource: "#AdminBundle/Controller/"
type: annotation
prefix: /admin
Then, all controllers/routes under AdminBundle will work under /admin prefix.
For example, IndexController/DefaultController/WhatEverController with this route inside AdminBundle:
/**
* #Route("/")
*/
public function indexAction()
{
//My code
}
Will match "/admin" instead of "/"
And:
/**
* #Route("/{page}")
*/
public function indexAction()
{
//My code
}
Will match "/admin/{page}" instead of "/{page}"
Since Symfony 5.1, you can define route priority :
/**
* #Route("/admin", name="admin", priority=10)
*/
/**
* #Route("/{slug}", name="pages", priority=99)
*/

How to route to two different 'Action' functions in same controller using annotation - Symfony2

Recently I shifted from Symfony 2.4 to Symfony 2.7
So I was following the new docs. Now say I have 2 action functions in same controller.
public function indexAction() {}
public function changeRateAction()
Previously I would have route them using routing.yml
change_interest_rate_label:
path: /change_interest_rate
defaults: { _controller: appBundle:appInterestRateChange:index }
change_interest_rate_action_label:
path: /change_interest_rate_and_archived_action
defaults: { _controller: appBundle:appInterestRateChange:changeRate }
Now in 2.7 docs, annotations are encourages. Inside controller file
/**
* #Route("/home", name="homepage")
*/
This will fire the action method contains in the controller file. But how can I write annotations for 2 methods for different urls included in the same controller file ?
That means I have indexAction & changeRateAction in same controller file. I want to route url /home with index function and /change with changeRate function. How to do this using annotations ? I know how to do this using routing.yml
You use the annotation routing on a method, not a controller, really.
You just specify the route before every method. In your case it would be something like this:
/**
* #Route("/home", name="homepage")
*/
public function indexAction() {
...
}
/**
* #Route("/change", name="changerate")
*/
public function changeRateAction() {
...
}
Be sure to read more about routing in the documentation: http://symfony.com/doc/current/book/routing.html

symfony2: documentation generated by NelmioApiDocBundle shows an additional route

I'm using NelmioApiDocBundle and FOSRestBundle to create an API.
In my routing.yml I have set prefix to /api.
The routes in my ApiController class are configured as follows:
/**
* #Get("/login/{username}/{password}")
* #ApiDoc(
* description="User Loggin",
* resource=true,
* parameters={
* {"name"="username", "dataType"="string", "required"=true, "description"="Username"},
* {"name"="password", "dataType"="string", "required"=true, "description"="Password"},
* }
* )
*/
public function loginAction($username, $password)
{
// ...
}
My problem is that the generated documentation shows these two routes:
/api/login/{username}/{password}
/login/{username}/{password}
I only want the route prefixed with /api to show up.
How can I achieve that ?
It's likely that NelmioApiDocBundle isn't aware of the prefix setting in your routing.yml.
As the bundle usually expects annotations you should add the #Prefix annotation at class-level instead.
use FOS\RestBundle\Controller\Annotations\Prefix;
/**
* #Prefix("/api")
*/
class ApiController
Don't forget to clear your cache after implementing the changes.
I found a solution, i override the template resources.html.twig of NelmioBundle and check manually if the uri contains /api or not

Symfony2 - Multiple slug in one route

In Symfony is it possible to access a controller through a route that potentially can contain indefinite number of slug, taking advantage from internal routing system?
e.g.
my_route:
pattern: /{slug_parent}/{slug_child}/{slug_nephew}/{slug_...}/...
as
www.mydomain.com/math/arithmetic/fractions
but also
www.mydomain.com/tech/android
It can be done, but instead of creating routes in config YML I would recommend to use #Route annotations. Add annotations in the controller class, like this:
/**
* #Route("/{slug_parent}/{slug_child}")
* #Route("/{slug_parent}/{slug_child}/{slug_nephew}/")
* #Route("/{slug_parent}/{slug_child}/{slug_nephew}/{slug_...}/")
*/
public function yourControllerAction()
{
...
}
http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html

symfony2 routing with annotations not working

I just downloaded symfony2 and am beginning to play with routing via annotations. I have my app/config/routing.yml set in the bundle I created to use annotations, and have deleted the Acme bundle and all routing references to it. That said, I've tried creating a couple different route annotations in my controller like #Route("/") and #Route("/hello/{name}") but I'm always greeted with a 404 error (using the dev environment). If I add the route in routing.yml it works just fine even though the routing is configured to use annotations. For whatever reason, my annotations are seemingly being ignored.
Here is my app/config/routing.yml:
DanDefaultBundle:
resource: "#DanDefaultBundle/Controller/"
type: annotation
prefix: /
And here is my controller method:
/**
* #Route("/")
* #Template()
*/
public function indexAction()
{
return array('name' => 123);
}
I've included the Sensio\Bundle\FrameworkExtraBundle\Configuration\Route namespace - everything as far as I can tell is correct with what I've seen in the documentation. What am I overlooking that's causing symfony2 to seemingly ignore my routing annotations? Again, if I add the routes to the routing yaml everything works so my bundle is working - but annotations seem to be ignored.
Thanks!
Dan
UPDATE:
It looks like I had to add the routes to routing_dev.yml in addition to routing.yml since I was operating in the dev environment. I suppose that's so you have have different routes in-between development and production? I suppose special care will have to be taken to make sure those routes stay in sync?
You accidentally removed the inclusion of routing.yml from routing_dev.yml.
if you use Route Prefix in your routing.yml
you must declare about your prefix above your class declaration like that :
/**
* #Route("/")
*/
class PostController extends Controller
{
/**
* #Route("/")
* #Template()
*/
public function indexAction()
{
}
/**
* #Route("{id}")
* #Template()
*/
public function showAction($id)
{
}
}
Like into Sensio FrameworkExtra Bundle Documentation

Categories