English 中文(简体)
在《国际仲裁裁决汇编》中控制器URI部分通过变量
原标题:Passing variables before controller URI segment in CodeIgniter

在URL控制人员部分之前,我谨通过一些全地点有效的变量。

例:

违约是:

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

有时 我也想提及用户创建该网址次配置(主题、菜单、......),这样用户就可以分享这个网站的URL冰理,而其他人则通过他的习惯配置看到该网站。

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

这里用户名base_url的定制部分。 它应当根据数据库加以验证,并作为届会变量,稍后在我的控制器和变化主题中使用。 该网站的所有链接也将在网站www.mysite.com/username上登入base_url后,在URL中加入用户名

解决这一问题的办法之一,就是把它当作:

controller/method/variable_name1/variable_value1/user_conf/username

......以及在我的项目中增加每个单一控制员的执行。 但这不是一个棘手的解决办法。

最佳回答

我在谈到这个问题一天之后,最后把海关路线加到我的项目中。 I m, working in CodeIgniter 2.0, so the place of this file should be 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;
    iii

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

    $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;
    iii

    // 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]);

iii

iii

问题回答

您是:

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

2. 如果您的所有职能定义都有用户名作为最后参数:

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 ;




相关问题
PHP Framework: Ebay Like Site

I am going to be builiding a site like ebay - with all the features of ebay. Please note my payment method is limited to paypal. What would be the best PHP framework to use to build this quickly, ...

What s the proper MVC way to do this....?

Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I m actually using Kohana). I m new to MVC and don t want to get this wrong... so I m wondering if i have ...

Check session from a view in CodeIgniter

What is the best way to check session from a view in CodeIgniter, it shows no way in their user guide, otherwise I will have to make two views on everything, which is kinda weird...still a newbie to ...

Using SimplePie with CodeIgniter and XAMPP

I am using CodeIgniter 1.7.2 with XAMPP 1.7.2 on a Windows computer. I am trying to make use of SimplePie. I followed all the instructions I could find: a copy of simplepie.inc is in my applications/...

CodeIgniter adding semicolons

How do I stop CodeIgniter adding semicolons ; to data sent via POST that contains ampersand &? For example it is converting "a=1&b=2&c=3" into "a=1&b;=2&c;=3". From looking on the forums ...

Best way to make Admin pages in CodeIgniter?

I m working on an app in CodeIgniter, and I want to have admin pages for several of the objects in the application, and I m wondering what would be the better way to put these into an MVC structure. ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签