在 CodeIgniter 中的控制器 URI 段之前传递变量

发布于 2024-12-17 07:25:48 字数 694 浏览 0 评论 0原文

我想在 URL 中的控制器段之前传递一些站点范围内经过验证的变量。

示例:

默认 URL 为:

www.mysite.com/controller/method/variable/

有时我还希望有这样的 URL 来引用用户创建的此站点的子配置(主题、菜单等),以便用户可以很好地共享此站点 URL,其他人也可以通过他的自定义配置查看该网站。

www.mysite.com/username/controller/method/variable

这里用户名base_url的自定义部分。它应该针对数据库进行验证,并将其设置为会话变量,以便稍后在我的控制器中使用它并更改主题。此外,在使用此用户名进入网站后,该网站上的所有链接都将开始使用www.mysite.com/username作为base_url网址。

解决这个问题的一种方法是像这样路由:

controller/method/variable_name1/variable_value1/user_conf/username

...并将实现添加到我的项目中的每个控制器中。但这不是一个优雅的解决方案。

I would like to pass some site-wide validated variables before controller segment in URL.

Example:

Default URL would be:

www.mysite.com/controller/method/variable/

Sometimes I'd like to have also URL like this to refer to user created sub-configuration of this site (theme, menus, ...), so user could share this site URL nicely and others would see the site though his custom configurations.

www.mysite.com/username/controller/method/variable

Here username is custom part of base_url. It should be validated against database and set as session variable to use it later in my controllers and change theme for example. Also all the links on the site would start to use www.mysite.com/username as base_url after website is entered with this username in the URL.

One way to solve this would be routing it like this:

controller/method/variable_name1/variable_value1/user_conf/username

...and the add the implementation to every single controller in my project. But this is not an elegant solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮是我掰弯的 2024-12-24 07:25:48

这就是您所追求的:

$route['(:any)/(:any)'] = '$2/$1';

所有函数定义都将用户名作为最后一个参数:

class Controller{function page(var1, var2, ..., varn, username){}}

或者,如果您只想在一个特定页面上,您可以执行以下操作:

$route['(:any)/controller/page/(:any)'] = 'controller/page/$2/$1'; //This will work for the above class.

或者,如果您希望它用于多个函数在控制器中你可以这样做:

$route['(:any)/controller/([func1|func2|funcn]+)/(:any)'] = 'controller/$2/$3/$1';

Is this what you're after:

$route['(:any)/(:any)'] = '$2/$1';

where all your function definitions have the username as the last parameter:

class Controller{function page(var1, var2, ..., varn, username){}}

Or, if you only want in on one specific page you could do something like this:

$route['(:any)/controller/page/(:any)'] = 'controller/page/$2/$1'; //This will work for the above class.

Or, if you want it for a number of functions in a controller you could do this:

$route['(:any)/controller/([func1|func2|funcn]+)/(:any)'] = 'controller/$2/$3/$1';
爱,才寂寞 2024-12-24 07:25:48

在解决这个问题一天之后,我最终将自定义路由器类添加到我的项目中。我正在使用 CodeIgniter 2.0,因此该文件的位置应该是 application/core/MY_Router.php

我的代码如下:

class MY_Router extends CI_Router {

// --------------------------------------------------------------------

/**
 * OVERRIDE
 *
 * Validates the supplied segments.  Attempts to determine the path to
 * the controller.
 *
 * @access    private
 * @param    array
 * @return    array
 */
function _validate_request($segments)
{
    if (count($segments) == 0)
    {
        return $segments;
    }

    // Does the requested controller exist in the root folder?
    if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
    {
        return $segments;
    }

    $users["username"] = 1;
    $users["minu_pood"] = 2;
    // $users[...] = ...;
    // ...
    // Basically here I load all the 
    // possbile username values from DB, memcache, filesystem, ...
    if (isset($users[$segments[0]])) { 
        // If my segments[0] is in this set
        // then do the session actions or add cookie in my cast.
        setcookie('username_is', $segments[0], time() + (86400 * 7));
        // After that remove this segment so 
        // rounter could search for controller!
        array_shift($segments); 
        return $segments;
    }

    // So segments[0] was not a controller and also not a username...
    // Nothing else to do at this point but show a 404
    show_404($segments[0]);

}

}

After messing with this problem for a day I ended up with adding custom router class to my project. I'm working in CodeIgniter 2.0, so the location of this file should be application/core/MY_Router.php

My code is following:

class MY_Router extends CI_Router {

// --------------------------------------------------------------------

/**
 * OVERRIDE
 *
 * Validates the supplied segments.  Attempts to determine the path to
 * the controller.
 *
 * @access    private
 * @param    array
 * @return    array
 */
function _validate_request($segments)
{
    if (count($segments) == 0)
    {
        return $segments;
    }

    // Does the requested controller exist in the root folder?
    if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
    {
        return $segments;
    }

    $users["username"] = 1;
    $users["minu_pood"] = 2;
    // $users[...] = ...;
    // ...
    // Basically here I load all the 
    // possbile username values from DB, memcache, filesystem, ...
    if (isset($users[$segments[0]])) { 
        // If my segments[0] is in this set
        // then do the session actions or add cookie in my cast.
        setcookie('username_is', $segments[0], time() + (86400 * 7));
        // After that remove this segment so 
        // rounter could search for controller!
        array_shift($segments); 
        return $segments;
    }

    // So segments[0] was not a controller and also not a username...
    // Nothing else to do at this point but show a 404
    show_404($segments[0]);

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文