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

  • SEARCH
  • Home
  • 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 6215401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:02:58+00:00 2026-05-24T07:02:58+00:00

I’m working on a small CakePHP application that is subject to the following constraint

  • 0

I’m working on a small CakePHP application that is subject to the following constraint (awkward but out of my control): I need it to work on either of two identical databases, with the choice being based on URL. For example:

http://www.example.com/myapp/foo/action/param
http://www.example.com/myapp/bar/action/param

The first obvious solution is to have two identical CakePHP applications at myapp/foo and myapp/bar with different database configurations. This has a kludgy feel to it, though, so I’m trying to find an elegant way of creating a single application.

The approach I’m considering is this: Define routes such that myapp/foo and myapp/bar will be associated with the same controller. Then give my DATABASE_CONFIG class a constructor:

function __construct() {
  $pathParts = explode('/', $_SERVER['REQUEST_URI']);
  if (array_search('foo', $pathParts)) {
    $this->default = $this->fooConfig;
  } else if (array_search('bar', $pathParts)) {
    $this->default = $this->barConfig;
  }
}

(Where of course I’ve defined fooConfig and barConfig for the two databases.) I do have control over the URL, so I can be confident that there won’t be extraneous occurrences of foo or bar in the URL.

My question is this: Is there a simpler, more elegant way of handling this odd situation? Maybe something in AppModel and/or AppController? Although I’m getting rid of duplicated code, I can’t shake the feeling that I’m replacing one code smell with another.

  • 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-05-24T07:02:59+00:00Added an answer on May 24, 2026 at 7:02 am

    There are a few ways to do this, here is one.

    Write a sweet custom route in which you always match:

    Router::connect('/:ds/*', array(), array('routeClass' => 'SweetDbRoute'));

    Then have SweetDbRoutes set a class variable you can use everywhere, including in your model constructors. Then it should fail so you don’t actually adjust the request.

    App::import('SweetDbClass', array('file' => '/path/to/your/sweet_db_class.php'));
    class SweetDbRoute extends CakeRoute {
        // put your failing route code here, but use your SweetDbClass to track datasource ...
        // see http://book.cakephp.org/view/1634/Custom-Route-classes
    }
    

    Then in your AppModel:

    App::import('SweetDbClass', array('file' => '/path/to/your/sweet_db_class.php'));
        class AppModel extends Model {
        public function __construct($id = false, $table = null, $ds = null) {
            $ds = SweetDbClass::$ds;
            parent::__construct($id, $table, $ds);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

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.