Sunday, May 24, 2015

Transition from shebang to pushState

I am using Angular.js and so far I used routes like www.oursite.com/#!/angularControllerRoute/angularAction/otherparameters

Where angularControllerRoute and angularAction are my way to indicate a generic route that I have on the frontend using the Angular UI Router.

I want to transition to use the HTML5 pushstate and this implies that my routes will be like: 

So far I had these routes: 

...

Router::connect(
    "/{$prefix}/:controller",
    array('action' => 'index')
);
Router::connect(
    "/{$prefix}/:controller/:action/*",
    array()
);

Router::connect(
    "/{$prefix}/**",
    array('controller' => 'Main', 'action' => 'index')
);

Now though, they are not going to work.

In particular all the /angularControllerRoute/angularAction/otherparameters must load
Main::index. Currently we get an exception says that angularControllerRoute is missing.

I thought to the following solutions: 
  • When a NoFoundException is raised and it is not an AJAX, load Main::index. I am throwing a few NoFoundException from the application and I am not sure what the implication may be.
  • Write a  Router::connect for each angularControllerRoute and point it to array('controller' => 'Main', 'action' => 'index') That is a little annoying since each time we add a route on the frontend we need to add a route in CakePHP.
  • Remove the default routes and add a custom route. Practically I wanted something like
    Router::connect("/{$prefix}/**",
                    array('controller' => 'Main', 'action' => 'index'),
                    array('routeClass' => 'CustomRoute')

    And check if the controller/action exists and if it does load that route, otherwise load Main::index. I think this would be my favorite solution.  What is the the fastest way to check if a Controller::Action exists? 
Am I missing something?

I am using CakePHP 2.5.3.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: