CodeIgniter Url重写类似于Pinterest和Twitter



我刚刚开始使用CodeIgniter,我正在创建一个用户登录的网站。我想知道如何将自定义名称写入登录用户的URL,类似于登录用户的用户名twitter和pinterest。

http://pinterest.com/username/

目前我有一个配置文件控制器,它只是有;

http://example.com/profile/

每个用户的

。有可能用codeigniter做到这一点吗?

要输出链接,您可以使用CI的URL帮助器。

你必须自动加载它,它应该在config/autoload.php:

$autoload['helper'] = array('url');

或者从你的函数中加载:

$this->load->helper('url');

然后可以使用anchor():

输出http://example.com/profile/这样的链接
$username = 'Corey'; // Set the user's name
anchor($username);

输出:

http://example.com/corey/

我假设您不想为数据库中的每个用户在控制器中创建一个函数。将魔术方法__call()添加到控制器中,并将用户名作为参数重定向到配置文件函数:

public function __call($name, $arguments)
{
    $this->profile($name);
}

但这还不够,你还必须做一点路由黑客。

最新更新