Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8241005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:41:11+00:00 2026-06-07T20:41:11+00:00

I’m trying to implement REST and parseExtension like functionality in my app, running on

  • 0

I’m trying to implement REST and parseExtension like functionality in my app, running on CakePHP 2.

Instead of having URLs like http://myapp.dev/controller/action.json I would like to use http://myapp.dev/json/controller/action.

The reason for this is that sometimes extensions look a little stupid when put onto something like http://myapp.dev/controller/index/show:10/page:2.json.

While this could be implemented in a custom route, I already have lots of custom routes, and do not want to have to create duplicates of each with a :type field in there for maintenance reasons.

What would be ideal is setting it up such that any url with /json /xml /html etc. in first place would be treated as if json, xml, html etc. were the extension.

While Prefix Routing looks ideal for this, it requires new methods (e.g. json_index, html_index etc. and I would have to specify each format as a separate prefix).

Is there any good way of doing this? I just want parseExtensions to instead be like a parsePrefixes method instead.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:41:14+00:00Added an answer on June 7, 2026 at 8:41 pm

    You should try the following :

    Router::connect('/:ext/', array(':ext'), array('pass' => 'ext'));
    Router::connect('/:ext/:controller/*', array(':ext'), array('pass' => 'ext'));
    Router::connect('/:ext/:controller/:action/*', array(':ext'), array('pass' => 'ext'));
    

    Then, the router will pass the :ext argument, in the “ext” value of the route parameters. Add it for all your rules !

    If you want to use traditionnel routes to work, you need to use a custom CakeRoute. Create a file “/libs/routes/RestRoute.php” in your app folder, add the following into it :

    class RestRoutes extends CakeRoute {
        function parse($url) {
            $params = parent::parse($url);
    
            if (empty($params)) {
                return false;
            }
    
            if(!in_array($params['ext'], Router::extensions())) {
                return false;
            }
    
            return $params;
        }
    }
    

    And in your /core/routes.php :

    App::import('Lib', 'routes/RestRoute');
    Router::connect('/:ext/', array(':ext'), array('pass' => 'ext', 'routeClass' => 'RestRoute'));
    Router::connect('/:ext/:controller/*', array(':ext'), array('pass' => 'ext', 'routeClass' => 'RestRoute'));
    Router::connect('/:ext/:controller/:action/*', array(':ext'), array('pass' => 'ext', 'routeClass' => 'RestRoute'));
    

    So, when your custom route won’t be able to pass an url, it would try the default url, without the ext parameters. Otherwise, the order of the parameters are not quiet allright.

    Maybe not the best or cleaner solution, but it’s a good start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.