Just imagine, I have a controller with several actions in it.
And to reach each action I have to define it strictly in routing.yml file like this:
admin_edit_routes:
pattern: /administrator/edituser
defaults: { _controller: MyAdminBundle:Default:edituser }
admin_add_routes:
pattern: /administrator/adduser
defaults: { _controller: MyAdminBundle:Default:adduser }
And I can have plenty of such pages. What I want to achieve is to define necessary action in my URI, just like Kohana routes do.
I mean, I want simply to use one route for all actions:
(code below is not valid, just for example)
admin_main_route:
pattern: /administrator/{action}
defaults: { _controller: MyAdminBundle:Default:{action} action:index}
It will pass all requests to /administrator/{anything} to this route, and after that, according to the action, stated in the uri, the needed action is gonna to be called.
If action is not stated in the uri, then we got thrown to index action.
Is it possible in Symfony 2 in any ways and if yes, then how?
Thanking in advance.
This doesn't feel right to do so. By exposing your controller API (methods) "on the fly" directly via GET method, you open your application to security vulnerabilities. This can also easily lead to unwanted behaviours.
Moreover, how would you generate routes in twig for example ? By giving explicit constants tied to the method names?
{{ path('dynamic_route', { 'method': 'addUser' }) }}
This is not how Symfony works nor what it recommands. Symfony is not CodeIgniter nor Kohana. You don't need a second "Front Controller"
If this is a laziness matter, you can switch your routing configurations to annotations. And let Symfony auto-generate the routes you need, with minimal efforts.
namespace Acme\FooBundle\Controller;
/**
* #Route("/administrator")
*/
class DefaultController extends Controller
{
/**
* #Route
*/
public function indexAction(Request $request);
/**
* #Route("/adduser")
*/
public function addUserAction(Request $request);
/**
* #Route("/edituser")
*/
public function editUserAction(Request $request);
/**
* #Route("/{tried}")
*/
public function fallbackAction($tried, Request $request)
{
return $this->indexAction($request);
}
}
The fallbackAction methods, returns any /administrator/* route which does not exist to the indexAction content.
To sum up, I advise not to use dynamic routing "on the fly" directly to method names.
An alternative would be to create a command which will generate routes once executed.
Well, maybe that's not the best way to do that, but it works. Your dynamicAction accepts action string parameter and checks if such action exists. If so, it executes given action with params.
/**
* Default Controller.
*
* #Route("/default")
*/
class DefaultController extends Controller
{
/**
* #Route("/{action}/{params}", name="default_dynamic")
*/
public function dynamicAction($action, $params = null)
{
$action = $action . 'Action';
if (method_exists($this, $action)) {
return $this->{$action}($params);
}
return $this->indexAction();
}
}
I'm curious if someone have any other ideas :>
Related
i'm new to Symfony. I'm trying to create dynamic universal route that will pick required controller based on part of url. I've found in docs that there is special param {_controller} that can be used in route pattern, but could not find any examples of usage.
// config/routes.yaml
api:
path: /api/{_controller}
So for example for route /api/product i expect ProductController to be initiated.
But as a result i get error "The controller for URI "/api/product" is not callable: Controller "product" does neither exist as service nor as class."
Can somebody please help me understanding how {_controller] param works? Or maybe there is a better way for specifying universal route that can dynamically chose controller without listing controller names in routes.yaml.
Thanks in advance
This isn't really the cleanest way to do what I think you're trying to do. If I am correct in assuming you want to have a /api/product/ point to methods in your product controller, then something like this is more "symfonyish"
// src/Controller/ProductController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/api/product", name="product_")
*/
class ProductController extends AbstractController
{
/**
* #Route("/", name="index")
* --Resolves to /api/product/
*/
public function index(): Response
{
// ...
}
/**
* #Route("/list", name="list")
* --Resolves to /api/product/list
*/
public function list(): Response
{
// ...
}
/**
* #Route("/{productID}", name="get")
* --Resolves to /api/product/{productID}
*/
public function get(string $productID): Response
{
// $productID contains the string from the url
// ...
}
}
Note that this really just scratches the surface of Symfony routing. You can also specify things like methods={"POST"} on the routing directive; so you could have the same path do different things depending on the type of request (e.g. you could have a route /product/{productID} that GETs the product on that request but updates the product on a PATCH request.
Regardless, the takeaway here is that it is unwieldy to have all of your routes defined in routes.yml rather you should define your routes as directives in the controller itself.
I've been developing a lot of my projects in the Laravel Framework for quite a while now, but not I'm working for a company who uses Symfony instead of Laravel.
On my way adapting to Symfony I got confused with the HTTP routing.
In Laravel you specify the HTTP method and link it to a route, like so:
Route::get('/', 'PageController#index');
Route::post('/', 'PageController#contact');
If you send a get request to '/' it will call the index method in PageController. If you send a post request it will call the contact method in PageController.
Now in Symfony I don't know how to do proper HTTP routing. I've seen people handle both get and post request in one method. For example:
public function index()
{
if ($request->isMethod('POST')) {
// handle post request and return something
}
// return something else
}
What I did, which looks a lot like the Laravel way is specifying 2 separate methods to handle each request:
/**
* #Route("/", name="homepage")
* #Method("GET")
*/
public function indexAction(Request $request)
{
// return the index page
}
/**
* #Route("/", name="contact")
* #Method("POST")
*/
public function contactAction()
{
// handle post request and return something
}
To summarize my question(s):
How to handle post requests in Symfony?
What are the best-practises to handle those requests?
What are naming conventions for such methods (indexPost/indexGet, indexAction/contactAction)?
Thanks in advance!
I'll try to go through each of your questions,
How to handle post requests in Symfony
Example with YML routes
my_route_post:
path: /route/path
defaults: { _controller: AppBundle:Foo:edit }
methods: [POST]
my_route_get:
path: /route/path
defaults: { _controller: AppBundle:Foo:show }
methods: [GET]
And you can define the controller actions :
class FooController extends Controller {
public function editAction(Request $request)
{
// POST request here
}
public function showAction()
{
// GET request here
}
}
What are the best-practises to handle those requests
I think both practises of creating a controller action to handle each HTTP request types or handling a POST request in the same method as GET are fine. Depending on which one fits your needs the best.
What are naming conventions for such methods (indexPost/indexGet, indexAction/contactAction)?
Each action method in a controller class is suffixed with Action
(again, this isn't required, but some shortcuts rely on this).
From : https://symfony.com/doc/current/controller.html
I want to learn symfony and I start to create a small application. I have a question related to the routes. So, I have in my project the routes :
/admin/homepage, /admin/news, admin/galery
No if I write in url /admin, this route doesn't exist and I get as error No route found for "GET /admin/". Exist a way to check if route doesn't exist and redirect to another route for example ? Thx in advance and sorry for my english
My routes :
news_all:
path: /news/all/{page}
defaults: { _controller: AppAdminBundle:News:all, page: 1 }
requirements:
page: \d+
_method: GET|POST
news_add:
path: /news/add
defaults: { _controller: AppAdminBundle:News:add }
In your case the best solution would be to override default ExceptionController and add custom logic there, e.g. redirection to other page - according to the docs: http://symfony.com/doc/current/cookbook/controller/error_pages.html#overriding-the-default-exceptioncontroller
# app/config/config.yml
twig:
exception_controller: AppBundle:Exception:showException
Note:
Instead of creating a new exception controller from scratch you can, of course, also extend the default ExceptionController. In that case, you might want to override one or both of the showAction() and findTemplate() methods. The latter one locates the template to be used.
Symfony's good practise are to set rout in your controller, using annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
and
/**
* #Route("/news/add", name="news_add")
*/
public function addAction()
{
// ...
}
Also, with annotation, you can set rout for a whole controller.
Which is, in your case, what you're looking for.
/**
* #Route("/admin")
*/
class NewsController extends Controller
{
/**
* #Route("/news/add", name="news_add")
*/
public function addAction()
{
// ...
}
}
Also, I advice you to take a look at #Template annotation.
Else, to answer your question, I think you can make a custom twig function (check this link for more information). Function that checks is the given name a valid route:
function routeExists($name)
{
// I assume that you have a link to the container in your twig extension class
$router = $this->container->get('router');
return (null === $router->getRouteCollection()->get($name)) ? false : true;
}
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
I have controller where I have two actions: createAction and showAction.
createAction creates form from form class and renders it to index.html.twig.
showAction makes database query and takes there some data and renders it to index.html.twig (same .twig file as before).
How I can have two actions in one route? I tried to do two same routes but different name in routing.yml, but it doesn't work. It only renders the first one.
(Sorry, bad english)
You can have the same URL for two separate actions as long as they respond to different http verbs (POST/GET/PUT/etc). Otherwise how would you expect the router to decide which action to choose?
Learn how to define http method requirements from the Adding HTTP Method Requirements section of the Routing documentation.
An example of annotation configuration:
class GuestbookController
{
/**
* #Route("/guestbook")
* #Method("POST")
*/
public function createAction()
{
}
/**
* #Route("/guestbook")
* #Method("GET")
*/
public function showAction()
{
}
}