【问题标题】:Custom Profile URL like <site url>/<username> Codeigniter自定义配置文件 URL,例如 <site url>/<username> Codeigniter
【发布时间】:2015-05-15 13:49:23
【问题描述】:

我想通过 www.siteurl.com/user/username 而不是 www.siteurl.com/user?name=username

获取用户的个人资料

这在 localhost 中完美运行,但在 live 服务器中却不行 它只是给出一个 404 Page Not Found,但在 localhost 中它指向请求的 url。如何在实时服务器中执行此操作?

这是我的代码

config.php 中启用了钩子

$config['enable_hooks'] = TRUE; 

然后创建一个 hooks.php 在 application->config 中添加以下代码

$hook=array(
        'pre_system' => array(
                array(
                        'class'    => 'Userlookup',
                        'function' => 'check_uri',
                        'filename' => 'Userlookup.php',
                        'filepath' => 'hooks',
                        'params'   => NULL,
                ),
        ),
);

然后,在 application-> 钩子中创建它包含的 Userlookup.php,

<?php defined('BASEPATH') or die('No direct script access allowed');

class Userlookup{

    protected $uri_username;
    protected $connection_method;

    protected $hostname;
    protected $username;
    protected $password;
    protected $database;

    public function __construct()
    {
        // Configure database connection
        include(APPPATH.'config/database'.EXT);
        $this->hostname = $db[$active_group]['hostname'];
        $this->username = $db[$active_group]['username'];
        $this->password = $db[$active_group]['password'];
        $this->database = $db[$active_group]['database'];

   }

    public function check_uri()
    {
         // First, we need get the uri segment to inspect
         $request_uri = explode('/',substr($_SERVER['REQUEST_URI'], 1));    //$request_uri[0] == username
         $this->uri_username = array_shift($request_uri);                   //string 'socialsite' (length=10)
         $connection_router = array_shift($request_uri);
         $this->connection_method = empty($connection_router) ? 'index' : $connection_router;
         // Connect to database, and check the user table
         mysql_connect($this->hostname, $this->username, $this->password) AND mysql_select_db($this->database);
         $res = mysql_query("SELECT user_Id FROM tbl_user WHERE user_name='".$request_uri[0]."'");
        // $id = mysql_result("SELECT user_Id FROM tbl_user WHERE user_name='".$request_uri[0]."'");
        // var_dump($id,0);die;

        // print_r($id);die;

         if ($row = mysql_fetch_object($res))
         {
                // If, there is a result, then we should modify server data
                // Below line means, we told CodeIgniter to load
                // 'User' controller on 'index', 'info' or any valid connection/method and we send 'id' parameter
                //$_SERVER['REQUEST_URI'] = '/user';
            $id = mysql_result($res,0);
                //print_r($id);die;
                $_SERVER['REQUEST_URI'] = '/user?id='.$id;
                //var_dump($_SERVER['REQUEST_URI']);die;

         }
         mysql_free_result($res);
    }

} 

这是我的 .htaccess 文件

RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

【问题讨论】:

  • 您在共享主机中吗?你能gotoanswer.stanford.edu/?q=Codeigniter+redirect+not+working在那里检查一些吗,如果你能阅读服务器错误日志,它也会对你有所帮助
  • not working 是什么意思,请说明任何错误或其行为。
  • 它给出 404 错误“找不到您请求的页面。”
  • 我认为问题在于你定义钩子使用的方式: $hook['pre_system'][] => array( 'class' => 'Userlookup', 'function' => 'check_uri' , 'filename' => 'Userlookup.php', 'filepath' => 'hooks', 'params' => NULL, ), );
  • 我认为你应该专注于你的 htaccess 和路由来解决这个问题。

标签: php codeigniter


【解决方案1】:

对于您所要求的,您不需要钩子、htaccess 或与路由文件混淆的事件。

由于您将控制器名称保留在 url 中,/users/[username] 您只需要定义 Codeigniter 提供的包罗万象的方法。

你的“Users.php”控制器中需要这样的东西:

public function _remap ($method, $params = array() ){
    if( method_exists($this, $method) ) {
        return call_user_func_array(array($this, $method), $params);
    }else{
        $Username = $method;
        $data['User'] = $this->users_model->get_by_username($Username);
        $this->load->view('users/profile', $data);
    }
}

【讨论】:

    【解决方案2】:

    最后,我创建了我想要的 URL,例如 example.com/username。在这里我将其发布为对其他人的帮助。

    For more

    我使用标准 MVC

    这种类型的虚 URL 涉及 URI 路由,因此为您的“application/config/routes.php”添加以下代码

    if($handle = opendir(APPPATH.'/controllers'))
    {
        while(false !== ($controller = readdir($handle)))
            {
                if($controller != '.' && $controller != '..' && strstr($controller, '.') == '.php')
                    {
                        $route[strstr($controller, '.', true)] = strstr($controller, '.', true);
                        $route[strstr($controller, '.', true).'/(:any)'] = strstr($controller, '.', true).'/$1';
                    }
            }
        closedir($handle);
    }
    
    // these are the /username routes
    $route['([a-zA-Z0-9_-]+)'] = 'controller/index/$1';
    

    然后在您的控制器中写入您可以从 GET 获取用户名。

    function index($username = '')
    {
        echo $username;
        //your code here
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      • 2010-11-08
      • 1970-01-01
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多