Last active
August 29, 2015 14:11
-
-
Save alecho/e7b573cc54140ba1706d to your computer and use it in GitHub Desktop.
Solving the 1 Route 2 Controllers problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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