1

就像 facebook(facebook.com/userurl) 我喜欢domain.com/username在 codeigniter 中使用。这里的用户名是唯一的。我想创建每个注册用户都有他/她自己的 url(路径)。有人可以帮忙吗?

4

3 回答 3

0

那不是网址。理解概念。你有一些基本问题。使用该 id 作为参数(GET 方法)。

看这张图片: 在此处输入图像描述

取得一些东西?对于更多...

作为你的点使用

$this->uri->segment(2);

关联

作为你的问题,

localhost/codeigniter/ripon

这是您的网址。因此,在您的默认控制器中, 方式 1:

public function index($user)
{
    echo "User is ".$user;
}

方式2:

public function index()
{
    $user = $this->uri->segment(2);
    echo "User is ".$user;
}
于 2013-10-24T10:31:45.587 回答
0

在任何自定义路由之前在 configs 的 routes.php 文件中添加以下代码

 $handle = opendir(APPPATH."/controllers");
 while (false !== ($file = readdir($handle))) {
 if(is_dir(APPPATH."/controllers/".$file)){
   $route[$file] = $file;
   $route[$file."/(.*)"] = $file."/$1";
 }
}

/*Your custom routes here*/

/*Wrap up, anything that isnt accounted for pushes to the alias check*/

$route['([a-z\-_\/]+)'] = "aliases/check/$1";

请参考这些问题 -我将如何在 codeigniter 中创建一个虚荣网址

简而言之,我们检查 URL 中是否存在控制器和函数名称。如果存在,则将其路由到该函数。否则,我们将发送给别名控制器并检查函数以查看用户是否使用该用户名存在。

于 2013-10-24T13:19:50.153 回答
0

我得到了http://www.aamra24.com/how-to-use-domain-then-username-in-codeigniter/的愿望解决方案

您需要将以下代码添加到 application/config/routes.php

$route['(.*)'] = "welcome/index/$1";
于 2013-10-27T12:07:20.083 回答