简体   繁体   English

以正确的方式在Codeigniter中进行路由

[英]Routing in codeigniter the right way

I'm developing my home page, this has a login button on the top right corner. 我正在开发我的主页,该页面的右上角有一个登录按钮。 I'd like to display a login page when the "login" text like so: 当“登录”文本如下时,我想显示一个登录页面:

<li class="nav-item">
                <a class="nav-link header-text" href="<?php echo 
site_url('Login/login') ?>">Login</a>
</li>

So the controller is called "Login" and the view is "login". 因此,该控制器称为“登录”,而视图称为“登录”。

My routing rules in the routes.php file are the following 我在routes.php文件中的路由规则如下

$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';

My base url is: http://localhost:8080/ My site url is: http://localhost:8080/index.php 我的基本网址是: http:// localhost:8080 /我的网站网址是: http:// localhost:8080 / index.php

And my .htaccess file looks like this: 我的.htaccess文件如下所示:

<IfModule authz_core_module>
    Require all denied
</IfModule>
<IfModule !authz_core_module>
    Deny from all
</IfModule>

The way this is currently setted up is not working for routing, i get a 404 not found message if i click the login and i get redirected to this url: http://localhost:8080/index.php/Login/login.html Can you please help me troubleshooting this problem? 当前设置的方法不适用于路由,如果我单击登录名并重定向到以下URL,则会收到404未找到的消息: http:// localhost:8080 / index.php / Login / login.html您能帮我解决这个问题吗? thanks! 谢谢!

EDIT: Here's the Login controller code: 编辑:这是登录控制器代码:

<?php
class Login extends CI_Controller {

    public function view($page_name = 'login')
    {
        if ( ! file_exists(APPPATH.'views/pages/'.$page_name.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }

        $data['title'] = ucfirst($page_name); // Capitalize the first letter

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page_name, $data);
        $this->load->view('templates/footer', $data);
    }

} }

Try removing $route['(:any)'] = 'pages/view/$1'; 尝试删除$route['(:any)'] = 'pages/view/$1'; and see what happens. 看看会发生什么。 It is grabbing every request and redirecting it to 'pages/view'. 它正在抓取每个请求并将其重定向到“页面/视图”。

I worry you misunderstand how CodeIgniter (CI) uses URLs. 我担心您会误解CodeIgniter(CI)如何使用URL。 You said, 你说,

So the controller is called "Login" and the view is "login". 因此,该控制器称为“登录”,而视图称为“登录”。

Which leads me to believe that URLs represent 这使我相信URL代表

example.com/class/view

when they actually represent this 当他们实际代表这个

example.com/class/function/id/

where 'id' is one or more values to be passed to the function. 其中“ id”是要传递给函数的一个或多个值。

Perhaps your comment was a misstatement, but if not I recommend rereading the CodeIgniter URLs section of the docs. 也许您的评论有误,但如果不是这样,我建议您重新阅读文档的CodeIgniter URLs部分。 You might also find the section URI Routing valuable as it clearly defines when you might want to use /config/Routes.php 您可能还会发现“ URI路由 ”部分很有价值,因为它清楚地定义了何时可能要使用/config/Routes.php的内容。

The login URL is quite strange too. 登录URL也很奇怪。 I'm curious to see what the controller code looks like. 我很好奇,看看控制器代码是什么样子。

Ok, I did it! 好吧,我做到了! Basically I had a wrong idea on the standard routing rules I created following the tutorial. 基本上,我对本教程之后创建的标准路由规则有一个错误的想法。 To solve the problem I had to: 1) change my base_url to: http://localhost:8080/app . 要解决此问题,我必须:1)将我的base_url更改为: http:// localhost:8080 / app I added the /app 2) The right way to route to a view in this case is: http://localhost:8080/app/index.php/name_of_the_class ; 我添加了/ app 2)在这种情况下,路由到视图的正确方法是: http:// localhost:8080 / app / index.php / name_of_the_class 3) to load the css for every page I had to use the base_url syntax in the link tag like so: " type="text/css" media="all"> 3)为每个页面加载CSS,我必须在链接标记中使用base_url语法,例如:“ type =” text / css“ media =” all“>

If you have any suggestion, best practice or something I can do better I'm here! 如果您有任何建议,最佳做法或我可以做得更好的事情,我在这里!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM