重新路由后默认控制器未加载

发布于 2024-09-11 19:59:51 字数 2790 浏览 3 评论 0原文

我有一个 codeigniter multistie 安装,其中有这样的代码,我可以为带有此类链接的站点提供服务

site.com/sites/sitename/controller/function/vars site.com/controller/function/vars/

subdom.site.com/controller/function/vars

的挑战是,我的路由

$route['sites/([^/]+)/(.*)'] = '$2';
$route['default_controller'] = "content";

这样的链接

可以使用像site.com/sites/sitename/controller/function/vars site.com/controller/function/vars

根据想法,当我访问

www.site.com/sites/sitename/

时,默认控制器未加载。

我制作了 config.php,以便关于链接结构,当我访问链接

site.com/sites/sitename/controller/function/vars

时,

$config['base_url']="http://site.com/sites/sitename";

如果我转到

site.com/controller/function/vars

那么

$config['base_url ']="http://site.com/";

对于第二种情况,默认控制器可以完美加载。对于子网站情况,没有/

我只得到 404

该怎么办?

更新 2:

我有一个多文件夹设置。

当用户访问 www.site.com/sites/site_name 时

应用程序 /root_folder/usersites/site_name 的文件夹

,将加载

。当用户仅访问 site.com/controller/function/var1/var2 时,

默认应用程序文件夹

会加载

/root_folder/application当用户访问 sub1.site.com 应用程序文件夹

/root_folder/domains/sub1_site_com 时,会加载

所以当我输入时在地址栏中

http://site.com/sites/site_name

它应该像没有 URI。并且应该加载默认控制器。

    // Application folder loading code
$myApp = '';


if($_SERVER['SERVER_ADDR']=='127.0.0.1'){
    $main_url='site.com';
}
else{
    $main_url='site1.com';
}

//echo $main_url;

switch($_SERVER['HTTP_HOST'])
{
    case $main_url;




        $uri_string=$_SERVER['REQUEST_URI'];
                    $link_way=explode('/',$uri_string);

        if(strlen($uri_string)>6 and $link_way[1]=='sites' ){



            //print_r($link_way);
            //var_dump($link_way);

            //checking if the link goes to usersites and sitename is bigger and =5
            if($link_way[1]=='sites' and strlen($link_way[2])>=5){


                $myApp='sites/usersites/'.$link_way[2];
                define('SITE_ALIAS','1|'.$link_way[2]);


                }
            elseif($link_way[1]=='sites' and strlen($link_way[2])<5){
                exit('Username should be more than 4 chars');

                }






            }

        else{

                        define('SITE_ALIAS','0|'.str_replace('.','_',$_SERVER['HTTP_HOST']));


        $myApp = 'application';
                }




        break;

     default:
        $myApp = str_replace('.','_',$_SERVER['HTTP_HOST']);
        $myApp=str_replace('www_','',$myApp);
                 define('SITE_ALIAS','2|'.$myApp);
        $myApp='sites/domains/'.$myApp;



    }

$application_folder = $myApp;

I have a codeigniter multistie install where I have such code that I can serve sites with such links

site.com/sites/sitename/controller/function/vars
site.com/controller/function/vars/

subdom.site.com/controller/function/vars

the challange is that , whith the routing

$route['sites/([^/]+)/(.*)'] = '$2';
$route['default_controller'] = "content";

I get working the links like

site.com/sites/sitename/controller/function/vars
site.com/controller/function/vars

By idea when I go to

www.site.com/sites/sitename/

the default controller is not loading.

I made the config.php so that regarding the link structure, when I visit link

site.com/sites/sitename/controller/function/vars

then

$config['base_url']="http://site.com/sites/sitename";

if I go to

site.com/controller/function/vars

then

$config['base_url']="http://site.com/";

for the second case the default controller loads perfectly. For the subsite case not/

I get just 404

What to do?

UPDATE 2:

I have a multifolder setup.

When user goes to www.site.com/sites/site_name

then a folder of application

/root_folder/usersites/site_name is loaded.

When user goes just site.com/controller/function/var1/var2

a default application folder which is

/root_folder/application is loaded

when user goes to sub1.site.com application folder

/root_folder/domains/sub1_site_com is loaded

So when I enter in address bar

http://site.com/sites/site_name

it should be like no URI. and should load default controller.

    // Application folder loading code
$myApp = '';


if($_SERVER['SERVER_ADDR']=='127.0.0.1'){
    $main_url='site.com';
}
else{
    $main_url='site1.com';
}

//echo $main_url;

switch($_SERVER['HTTP_HOST'])
{
    case $main_url;




        $uri_string=$_SERVER['REQUEST_URI'];
                    $link_way=explode('/',$uri_string);

        if(strlen($uri_string)>6 and $link_way[1]=='sites' ){



            //print_r($link_way);
            //var_dump($link_way);

            //checking if the link goes to usersites and sitename is bigger and =5
            if($link_way[1]=='sites' and strlen($link_way[2])>=5){


                $myApp='sites/usersites/'.$link_way[2];
                define('SITE_ALIAS','1|'.$link_way[2]);


                }
            elseif($link_way[1]=='sites' and strlen($link_way[2])<5){
                exit('Username should be more than 4 chars');

                }






            }

        else{

                        define('SITE_ALIAS','0|'.str_replace('.','_',$_SERVER['HTTP_HOST']));


        $myApp = 'application';
                }




        break;

     default:
        $myApp = str_replace('.','_',$_SERVER['HTTP_HOST']);
        $myApp=str_replace('www_','',$myApp);
                 define('SITE_ALIAS','2|'.$myApp);
        $myApp='sites/domains/'.$myApp;



    }

$application_folder = $myApp;

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

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

发布评论

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

评论(3

静待花开 2024-09-18 19:59:51

您似乎正在做的是寻找具有您正在通过的“站点名称”的控制器。因此,如果您导航到 site.com/sites/my-site/,您的路线会告诉它寻找名为 my-site 的控制器并运行 index 方法。

路由的值应该是实际控制器/方法对的路径。

$route['sites/([^/]+)/(.*)'] = '$2';

应该是

$route['sites/([^/]+)/(.*)'] = 'sites/index/$1/$2';

这是假设它是接受站点名称作为站点控制器中的第一个参数的索引方法。

What you appear to be doing is looking for a controller with the 'sitename' you are passing through. So if you navigate to site.com/sites/my-site/ you route tells it to look for a controller called my-site and run the index method.

The value of the route should be a path to an actual controller / method pair.

$route['sites/([^/]+)/(.*)'] = '$2';

should be

$route['sites/([^/]+)/(.*)'] = 'sites/index/$1/$2';

This is assuming it's the index method that accepts the sitename as it's first parameter in your sites controller.

月亮是我掰弯的 2024-09-18 19:59:51

目前尚不完全清楚您要问什么,但这里有一个镜头:

您需要在网站的根目录中创建一个 .htaccess 文件(即在您的系统同一文件夹中) code> 文件夹位于)。在该文件中:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

请参阅本页的“删除 index.php 文件”部分: http:// /codeigniter.com/user_guide/general/urls.html 了解更多信息。

此外,您使用的 route 将使您在访问 www.site.com 时看到 www.site.com/网站/内容

对于 URL www.site.com/sites/sitename/content/sites 是您的控制器,sitename 是方法或函数,< code>content 将被视为 sitename 函数的参数 - 这不会像您想要的那样工作,但在没有看到您的控制器的情况下我无法确定。

也许编辑您的问题并添加您的控制器,我们可以提供更多帮助。


更新:

1: $config['base_url'] 与路由或正在使用的控制器无关,因此这使您的问题更难理解。

2:不清楚你想要实现什么(抱歉)。

当我去的时候想到

www.site.com/sites/sitename/

默认控制器未加载。

根据 CI 用户指南

CodeIgniter 可以被告知加载一个
当 URI 不是时的默认控制器
存在,就像只有当
请求您的站点根 URL。到
指定默认控制器,打开
你的应用程序/config/routes.php
文件并设置此变量:

因此,这意味着仅当不存在 URI 时才使用 default_controller。换句话说:默认控制器仅在 URL 为 www.site.com 时适用,在其他情况下不会使用它(除非您使用控制器文件夹中的子文件夹 - 见下文)。

如果您尝试使每个站点都有自己的控制器,则可以使用控制器文件夹中的子文件夹。

在routes.php中:

$route['sites/(:any)'] = "$1";
$route['default_controller'] = "content";

然后你的文件夹结构:

这样你就有了控制器文件夹。在其中,为每个站点创建一个文件夹。在每个控制器中创建默认控制器(上图中名为 content.php)。

通过此设置,www.site.com/sites/site1 将从 application/controllers/site1/content.php 调用默认控制器 (content) code> 并显示该控制器的 index 函数。

如果您随后想要调用 site1 控制器的其他函数,则 URL 将如下所示:
www.site.com/sites/site1/content/otherFunction

希望这有帮助。

It's not totally clear what you're asking, but heres a shot:

You need to create an .htaccess file in the root of your site (i.e. in the same folder that your system folder is in). In that file:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

See the "Remove the index.php file" section of this page: http://codeigniter.com/user_guide/general/urls.html for more info.

Also, the route that you're using would make it so that when you go to www.site.com, you will see www.site.com/sites/content.

With the url www.site.com/sites/sitename/content/, sites is your controller, sitename the method or function, and content would be considered a parameter to the sitename function -- this won't work like it seems like you want, but I can't be sure without seeing your controller.

Maybe edit your question and add your controller(s), and we can be of more assistance.


UPDATE:

1: $config['base_url'] has nothing to do with routing or which controller is being used, so this is making your question harder to understand.

2: It isn't clear what you are trying to accomplish (sorry).

By idea when I go to

www.site.com/sites/sitename/

the default controller is not loading.

According to the CI user guide:

CodeIgniter can be told to load a
default controller when a URI is not
present, as will be the case when only
your site root URL is requested. To
specify a default controller, open
your application/config/routes.php
file and set this variable:

So, what this means is that the default_controller is used only when there is no URI present. In other words: the default controller only applies when the URL is www.site.com, and in no other case will it be used (unless you are using sub-folders in the controllers folder -- see below).

If you trying to make it so that each of your sites has its' own controller, you could use subfolders in your controller folder.

In routes.php:

$route['sites/(:any)'] = "$1";
$route['default_controller'] = "content";

Then your folder structure:

So you have your controller folder. In it, create a folder for each site. In each of those controllers create your default controller (named content.php in the above image).

With this setup, www.site.com/sites/site1 will call the default controller (content) from application/controllers/site1/content.php and show the index function of that controller.

If you then want to call other functions of the site1 controller, the URL would look like:
www.site.com/sites/site1/content/otherFunction.

Hope this helps.

兔小萌 2024-09-18 19:59:51
$uri_string=$_SERVER['REQUEST_URI'];
                    $way=explode('/',$uri_string);


                       /// print_r($way);

//echo $way[3];
                        if($way[1]=="sites" and strlen($way[2])>2 and strlen($way[3])<1){


                            echo "JAJA ";


$route['sites/:any'] = "content/show_page/home";
                        }
                        else{



                           $route['sites/([^/]+)/(.*)'] = '$2';
                        }

这是解决方案。感谢所有回答的人。谢谢暴风雨。您在路由示例中向我指出了写入方向。谢谢

$uri_string=$_SERVER['REQUEST_URI'];
                    $way=explode('/',$uri_string);


                       /// print_r($way);

//echo $way[3];
                        if($way[1]=="sites" and strlen($way[2])>2 and strlen($way[3])<1){


                            echo "JAJA ";


$route['sites/:any'] = "content/show_page/home";
                        }
                        else{



                           $route['sites/([^/]+)/(.*)'] = '$2';
                        }

this was solution. thanks all who answered. thanks stormdrain. you pointed me to a write direction in your routing example. Thanks

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