Skip to content

Instantly share code, notes, and snippets.

@alecho
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alecho/e7b573cc54140ba1706d to your computer and use it in GitHub Desktop.
Save alecho/e7b573cc54140ba1706d to your computer and use it in GitHub Desktop.
Solving the 1 Route 2 Controllers problem
<?php
// Given you want to perform one of two or more actions with a single url.
//
// in Config/routes.php
Router::connect('/:username/:action', array('controller' => 'users', 'action' => 'userOrShop'));
// Your UsersController.php might look something like this then.
class UsersController extends AppController {
public function userOrShop() {
// Do whatever you need to do to determine if the requested user is a
// "shop" or just a user here.
if ($this->User->isShop()) {
return $this->_doShopThing();
}
return $this->_doUserThing();
}
protected function _doShopThing() {
// This is where your regular controller logic would go for when the user
// is a shop like "BobsShop.
}
protected function _doUserThing() {
// This is where your logic for a username of "Bob" would go.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment