Set conditions on routing rules in symfony 1.x - php

With symfony 1.4 I'm using this rule:
asasds:
url: /users/:order
param: { module: users, action: index }
I want to achieve this: www.mysite.com/users/aNumericValueAsOrder so far it looks ok, but what if I have an action www.mysite.com/users/createOrder as FORM POST action. createOrder() is a method, not an numeric id.
How to distinguish? How to tell it to the routing system that "please check whether it is an action"?

Try this:
asasds:
url: /users/:order
param: { module: users, action: index }
requirements: { order: \d+ }
That's for numeric values, of course; the requirements subparams are regular expressions, so are very flexible.

Related

skipping controller name from URL in symfony 1.4

I've an existing project in symfony 1.4
For static pages, we use a module Page which give following URLs:
http://www.example.com/page/aboutus
http://www.example.com/page/faq
http://www.example.com/ (page/index method, points to method page/home)
For new requirements, we need URL in format:
http://www.example.com/aboutus
http://www.example.com/faq
http://www.example.com/ (index/home method of page module; working as page is default module)
There are 16 such static pages and all are listed under page module. As of now I'm planning to create 16 new modules with putting each static page in index method of new module but I guess its a bad solution and hope symfony is definitely not designed to be used in this way.
Is there any way to skip module name from URL, at least for default module?
You might want to use a rewrite rule as explained in
http://corz.org/serv/tricks/htaccess2.php
Use the route feature of symfony!
In routing.yml:
aboutus_page:
url: /aboutus
param: { module: page, action: aboutus }
faq_page:
url: /faq
params: { module: page, action: faq }
home_page:
url: /
params: { module: page, action: index }
# your other routes
# ...
The actions.class.php of your page module will look like:
class pageActions extends sfActions
{
public function executeAboutus (sfWebRequest $request)
{
// ...
}
public function executeFaq (sfWebRequest $request)
{
// ...
}

Routing in Symfony2

How to setup default routing in Symfony2?
In Symfony1 it looked something like this:
homepage:
url: /
param: { module: default, action: index }
default_symfony:
url: /symfony/:action/...
param: { module: default }
default_index:
url: /:module
param: { action: index }
default:
url: /:module/:action/...
I was looking through the cookbook for an answer to this, and think I've found it here. By default, all route parameters have a hidden requirement that they match any character except the / character ([^/]+), but this behaviour can be overridden with the requirements keyword, by forcing it to match any character.
The following should create a default route that catches all others - and as such, should come last in your routing config, as any following routes will never match. To ensure it matches "/" as well, a default value for the url parameter is included.
default_route:
pattern: /{url}
defaults: { _controller: AcmeBundle:Default:index, url: "index" }
requirements:
url: ".+"
I don't think it's possible with the standard routing component.
Take a look to this bundle, it might help :
https://github.com/hidenorigoto/DefaultRouteBundle
// Symfony2 PR10
in routing.yml:
default:
pattern: /{_controller}
It enables you to use this kind of urls: http://localhost/MySuperBundle:MyController:myview
Symfony2 standard routing component does not support it, but this bundle fills the gap Symfony1 left:
https://github.com/LeaseWeb/LswDefaultRoutingBundle
It does what you expect. You can default route a bundle using this syntax:
FosUserBundle:
resource: "#FosUserBundle"
prefix: /
type: default
It scans your bundle and automatically adds routes to your router table that you can debug by executing:
app/console router:debug
Example of automatically added default routes:
[router] Current routes
Name Method Pattern
fos_user.user.login_check ANY /user/login_check.{_format}
fos_user.user.logout ANY /user/logout.{_format}
fos_user.user.login ANY /user/login.{_format}
...
You see that it also supports the automatic "format" selection by using a file extension (html, json or xml).
Here is an example: http://docs.symfony-reloaded.org/master/quick_tour/the_big_picture.html#routing
A route definition has only one mandatory parameter pattern and three optionals parameters defaults, requirements and options.
Here's a route from my own project:
video:
pattern: /watch/{id}/{slug}
defaults: { _controller: SiteBundle:Video:watch }
requirements: { id: "\d+", slug: "[\w-]+"
Alternatively, you can use #Route annotation directly in a controller file. see https://github.com/sensio/SensioFrameworkExtraBundle/blob/master/Resources/doc/annotations/routing.rst
As for the default routes, I think Symfony2 encourages explicit route mapping.
Create a default route is not a good way of programming. Why? Because for this reason was implemented Exception.
Symfony2 is built just to do right things in the right way.
If you want to redirect all "not found" routes you should use exception, like NotFound404 or something similar. You can even customise this page at your own.
One route is for one purpose. Always. Other think is bad.
You could create your own bundle that handled all requests and used URL parameters to construct a string to pass to the controller's forward method. But that's pretty crappy, I'd go with well defined routes, it keeps your URLs cleaner, and decouples the URL and controller names. If you rename a bundle or something, do you then have to refactor your URLs?
If you want to create a "catch all", your best bet would be to hook on the KernelEvents::EXCEPTION event. This event gets triggered whenever an Exception falls through to the HttpKernel, this includes the NotFoundHttpException thrown when the router cannot resolve a route to a Controller.
The effect would be similar to Symfony's stylized 404 page that gets rendered when you send the request through app_dev.php. Instead of returning a 404, you perform whatever logic you're looking to.
It depends... Some of mine look like this:
api_email:
resource: "#MApiBundle/Resources/config/routing_email.yml"
prefix: /
and some look like
api_images:
path: /images/{listingId}/{width}/{fileName}
defaults: { _controller: ApiBundle:Image:view, listingId: null, width: null, fileName: null }
methods: [GET]
requirements:
fileName: .+

Symfony routing with an API Web Service

I'm finishing the API of our web service. Now I'm thinking on how to make the route changes, so if we decide to make a new version we don't break the first API.
right now:
url: /api/:action
param: { module: api, action: :action }
requirements:
sf_format: (?:xml|json)
what i've thought:
url: /api/v1/:module/:action
param: { module: api1, action: :action }
requirements:
sf_format: (?:xml|json)
url: /api/v2/:module/:action
param: { module: api2, action: :action }
requirements:
sf_format: (?:xml|json)
That's easy, but the perfect solution would be to have the following kind of route
# Automatically redirects to one module or another
url: /api/v:version/:module/:action
param: { module: api:version, action: :action }
requirements:
sf_format: (?:xml|json)
any ideas on how to do it? what do you recommend us to do?
thanks!
I think the approach of having 2 routes, one with v1 on it and another with v2 is the better solution. It may sound like redoing work, but if u start to think, the reason to have to versons is that the first is imcompatible with second. So, mix the 2 logics would be an overkill. If you think in some day in the future when u can have 3 version, how do you think your logic will look like?
The better solution is to make the both separate, so if u need to stop doing support for version 1 it will be easy as remove the file for version 1. =)
How about just use the old routing rules and append .xml1/.xml2/.json1/.json2 to the end? That way you can reuse your current modules and only need to create a new view ("indexSuccess.xml1.php").
Or create a new routing class?
You should probably create a custom route collection, as shown here.
This would allow you to match any routing needs you have. You could move the version logic to the route class and route collection class.

Passing Params to Symfony Routes

I have a defined route that displays a dynamic page:
page_show:
url: /:domain_slug/:slug
class: sfPropelRoute
options:
model: Page
type: object
method: doSelectByDomain
param: { module: page, action: show }
requirements:
sf_method: [get]
This works great, but now I want my homepage URI to route to a specific page. To do that, I assume that I have to pass the :domain_slug and :slug values of the page I want to display as the homepage. That's fine, but I can't seem to track down any documentation or example that shows me how to go about doing that.
Is it possible to specify specific variable values in a route? In this case, I want to pass :domain_slug => portal, :slug => dashboard (that syntax doesn't work, btw). Essentially, I want to create a homepage route that looks something like this:
homepage:
url: /
class: sfPropelRoute
param: { module: page, action: show, array( :domain_slug => portal, :slug => dashboard ) }
options:
model: Page
type: object
method: doSelectByDomain
But different enough that it, you know, works. :-) I suppose I could create a simple route to a different method, modify the request parameters manually and forward to the executeShow() method, but that's a hack I'd rather avoid if a more elegant solution is available.
Thanks.
You can define values in the param key of the route... for example:
homepage:
url: /
class: sfPropelRoute
param: { module: page, action: show, domain_slug: portal, slug: dashboard}
options:
model: Page
type: object
method: doSelectByDomain
At least thats how it works with a non propel/doctrine route. I assume it should be the same with any type of route in the framework.
I never found a way to do this that worked (or maybe I just couldn't manage to do it correctly). My solution was to create the route and pass it through a custom method that does the work to specify the appropriate page details:
homepage:
url: /
class: sfPropelRoute
options:
model: Page
type: object
method: doSelectHomepage
param: { module: page, action: show ) }
requirements:
sf_method: [get]
The doSelectHomepage() method does all of the work I was hoping I'd be able to do by hardcoding values in the route itself.
Actually it did work, it was just the order in which the routes were defined. The first answer is correct. It may have worked for you if you added "homepage" rule BEFORE the "page_show" rule. So maybe Symfony isn't as stupid as I thought...No, no, it is. It's just not that bad.

Can you force symfony resolve to a specific route?

Say that I have to following 2 routes in this order:
Zip:
url: home/:zip
param: { module: home, action: results }
State:
url: home/:state
param: { module: home, action: results }
and I use a route such as:
'#State?state=CA'
Why does it resolve to the Zip route?
I thought when you specified the route name explicitly: '#State' that it did not parse the entire routing file and just used the specific route you asked for.
I would like to be able to use the same action to display data based on the variable name (zip or state) I don't want to have to create the same action (results) twice just to pass in a different parameter.
You need to add requirements so that it will recognize the different formats
Zip:
url: home/:zip
param: { module: home, action: results }
requirements: { zip: \d{5} } # assuming only 5 digit zips
State:
url: home/:state
param: { module: home, action: results }
requirements: { state: [a-zA-Z]{2} }
That should fix it, although I agree with you that when you use a route name in a helper it should use the named route.
the problem here is that both routing rules resolves to somewhat same url ( /home/) and when generating links it uses correct named route to generate url.
when you click on that generated url( for example /home/CA) withouth requirements addition it will match the first rule in routing.yml file that matches and that is your "Zip" route.
just to explain what is happening.
It does use the named route. The issue is that your URLs are the same (the paramater name is irrelevant) so when you load the URL in your browser, the first route to match will always be Zip as there is no way for Symfony to know which one you want.
Adding requirements: is the way to go.

Categories