好吧,路由是有效的 - 替代方案是 remapping your controllers。
让我们来看看这两种可能性。
一个想象的情况:
稍后,您希望允许您的用户在他们的个人资料上显示徽章/奖牌/成就/某些东西。
使用路由,可以这样实现:
$route['player/(:any)/(:any)'] = "player/show_$2/$1";
$route['player/(:any)'] = "player/show_profile/$1";
你的控制器可能看起来像这样:
class Player extends CI_Controller
{
public function show_profile( $username )
{
// the profile info
}
public function show_badges( $username )
{
// the profiles badges
}
public function show_scores( $username )
{
// the profiles scores
}
}
}
基本上,这允许您在控制器中简单地添加另一个方法,在该方法前加上 show_(如 public method show_friends( $username )),然后您可以通过转到 /player/SomeDude/friends立即访问它>
看看替代方案,重新映射您的控制器将允许您不使用路由,而是像这样编写一个控制器:
class Player extends CI_Controller
{
public function _remap($username, $params = array())
{
if(empty($username))
show_404();
$this->user = $this->user_model->find($username);
if(count($params) == 0)
$method = 'index';
else
$method = $params[0];
unset($params[0]); //No need to send the method along as a parameter
$method = 'process_'.$method;
if (method_exists($this, $method))
{
return call_user_func_array(array($this, $method), $params);
}
show_404();
}
public method process_index()
{
// the profile info
}
public method process_badges()
{
// the profiles badges
}
public method process_scores()
{
// the profiles scores
}
}
就个人而言,我喜欢路由。我认为它是透明的,让我的控制器看起来更干净。