Deep Wildcard Sailsjs - sails.js

How do I define deep wildcards in sailsjs?
For example, I can create a wildcard in mysite.com/slug/wildcard by adding /slug/:wildcard/ to my route.
However, this doesn't work: mysite.com/slug/wildcard/extended/. I want to be able to capture all the routes that start with slug.
Is that possible?

I think this is possible with the current revision of sails, as route parameters are defined using :param .. each instance of :something indicates a new param for the route handler. These are not called wildcards but parameters.
I know express supports * for a wildcard, have you tried something like this...
'/slug/*': {
controller: 'Index',
action: 'index'
},

Related

Zend routing: removing null parameters from the URL

If I have a route which is defined like this:
/event/:id/:instance_id/view
It appears in the URL like this when both parameters are set.
example.com/event/1/15/view
However, instance_id is optional, and therefore may be null, with the URL looking like this (as created by the url helper):
example.com/event/1//view
I want it to look like this:
example.com/event/1/view
How do I remove the unnecessary forward slash?
The above question is for Zend 1 and the below solution is for Zend 2, so please dis-regard my answer.
Your route does not say 'instance_id' is optional. You can achieve this by doing this
'route' => '/event/:id[/:instance_id]/view',
It should fix the url problem. Also ideally, 'view' should be after 'event', if it is possible in your scenario.
'route' => '/event/view/:id[/:instance_id]',
Any fix text should ideally come up first in the route and anything optional last.

Use route prefix with RESTful routes in CakePHP

Working on building an API and would like to use RESTful routes.
I got it to work just fine like this:
http://www.mysite.com/events.json // returns json results with my events
http://www.mysite.com/events/123.json // returns json results with event of id '123'
BUT - I want to be able to do this using an 'api' prefix.
So, I added the api Routing prefix:
Configure::write('Routing.prefixes', array('admin', 'api'));
And changed my actions from 'view' and 'index' to 'api_view' and 'api_index'.
But now it doesn't work. (eg. I have to write the action name or it won't find the correct one based on HTTP.
The end goal would be to be able to do something like this:
GET http://www.mysite.com/api/1.0/events.json // loads events/api_index()
GET http://www.mysite.com/api/1.0/events/123.json // loads events/api_view($id)
DELETE http://www.mysite.com/api/1.0/events/123.json // loads events/api_delete($id)
...etc
I ended up having to just write the routes manually:
Router::parseExtensions('json', 'xml');
Router::connect('/api/:version/:controller/:id/*',
array('[method]'=>'GET', 'prefix'=>'api', 'action'=>'view'),
array('version'=>'[0-9]+\.[0-9]+', 'id'=>'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}'));
Router::connect('/api/:version/:controller/*',
array('[method]'=>'GET', 'prefix'=>'api', 'action'=>'index'),
array('version'=>'[0-9]+\.[0-9]+'));
Router::connect('/api/*', array('controller'=>'events', 'action'=>'index', 'ext'=>'html'));
Notes:
The [method] is what forces the HTTP type (eg. RESTful)
The parseExtensions() makes it so you can have it display the data in different formats automatically by changing the extension in your URL.
The last Router:: line was just a catchall for anything /api/ that didn't match - it forwarded it to the homepage. Eventually I'll probably just route this to an API error page.
The 'ext'=>'html' of the last Router:: line was to keep parseExtensions from trying to use whatever extension was in the URL - if it's redirecting for reasons they made the call wrong, I just want it to go back to the homepage (or whatever) and use the normal view.
Try something like this.
Router::connect('/:api/:apiVersion/:controller/:action/*',
array(),
array(
'api' => 'api',
'apiVersion' => '1.0|1.1|'
)
);
With prefix routing
Router::connect('/:prefix/:apiVersion/:controller/:action/*',
array(),
array(
'prefix' => 'api',
'apiVersion' => '1.0|1.1|'
)
);
Will match only valid API versions like 1.0 and 1.1 here. If you want something else use a regex there.
I know this is an old post, but there is a routing method called mapResources which creates the special method based routing for you.
http://book.cakephp.org/2.0/en/development/rest.html
You put it in routes.php like so:
Router::mapResources(array('controller1', 'controller2'));
The docs have a nice little table showing how the requests are mapped to different actions, which you can always override if you need to.

Different controllers depending on GET parameter

In mojolicious app I want define different controller action for url depend on get parameter.
In pseudocode it is:
#test.pl?command=check&id=1
$r->route('/test.pl?command=check')->via('GET')->to(controller => 'bar', action => 'check');
#test.pl?command=confirm&id=12&amount=100
$r->route('/test.pl?command=confirm')->via('GET')->to(controller => 'bar', action => 'confirm');
What I should use? Some kind of bridges? Help me with example please.
It looks like you can't change your url pattern so one way would be to route to an action like bar::command which then calls the bar::check() or bar::confirm() actions depending on the value of the command parameter

Why Zend_View_Helper_Url returns relative path when using the default route and absolute path when using a custom route?

I have an application running on Zend Framework 1.12 and I am using the URL view helper. I also have a few custom routes which I use with the URL helper to clean the code.
When I use a format such as this:
$this->url(array('module' => 'myapp', 'controller' => 'index', 'action' => 'index'), 'default', true)
The resulting string is a relative path to the action - /myapp/index/index
But when I use a custom route as such:
$this->url(array('var' => 'value'), 'custom-route', true)
The resulting string is an absolute path - http://www.domain.com/custom-route
I don't understand why is this happening and I can't find any information about this behavior online. Furthermore, I wish to know if there's a way to prevent it from happening and produce consistency in the way the URL view helper works (preferably to always return a relative path).
Thanks.
I think this is as simple as your first example being a default type Zend_Controller_Router_Route_Module route that is normally relative.
Your second example is a named route so it needs to include the base url in order to be valid. I don't believe rewritten routes can be relative as the route may or may not refer to an actual location.
I've done some limited testing and I believe that if you set your baseUrl value the first example will also include the host address. So for consistency your urls will always be absolute.
//application.ini
resources.frontController.baseurl = http://www.domain.com
Hope this helps.

Zend-Route for ajax API

I'm trying to add a route to my application, so that I can use it with ajax calls.
Here is what I have in my application.ini
;Routes
resources.router.routes.products.route = "/backend/api/:command"
resources.router.routes.products.defaults.module = "backend"
resources.router.routes.products.defaults.controller = "api"
resources.router.routes.products.defaults.action = "index"
When a ajax call is made, to /backend/api/SomeCommand, the following error is produced:
Message: Invalid controller specified (backend)
array (
'controller' => 'backend',
'action' => 'maestro',
'module' => 'default',
)
as you can see module has been set to "default", instead of "backend", and controller is "backend" instead of "api", what could have caused this?
Looks like you've got another more generic route defined after this one that's matching the request.
You need to define your routes in order of least to most specific, specificity usually being improved by the presence of fixed terms like your backend/api prefix.
See Basic Rewrite Router Operation, in particular
Note: Reverse Matching
Routes are matched in reverse order so make sure your most generic routes are defined first.
FYI: You don't need to prefix your routes with a forward-slash