【问题标题】:How to use domain.com/username in codeigniter?如何在 codeigniter 中使用 domain.com/username?
【发布时间】:2013-10-24 10:22:28
【问题描述】:

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

【问题讨论】:

    标签: php codeigniter url uri codeigniter-url


    【解决方案1】:

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

    查看此图片:

    得到什么?对于more...

    根据你的观点使用

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

    Link

    作为你的问题,

    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;
    }
    

    【讨论】:

    • 这里“hello”是控制器名称,“you”是函数名称。我不想在url中给它,我只想给用户名作为参数
    • 虚荣网址只是技术词。别担心。你有没有?如果没有,告诉我你停在哪里?并给出一些代码。
    • 告诉我如何在输入localhost/codeigniter/ripon 时回显用户名。这里 ripon 是用户名。考虑 CodeIgniter_2.1.4 的新下载副本。
    【解决方案2】:

    在 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";
    

    请参考这些问题 - How would i create a vanity url in codeigniter

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

    【讨论】:

      【解决方案3】:

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

      您需要在 application/config/routes.php 中添加以下代码

      $route['(.*)'] = "welcome/index/$1";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-05
        • 2011-02-24
        相关资源
        最近更新 更多