简体   繁体   English

网址无法在Codeigniter中使用

[英]URLs not working in codeigniter

Hi I'm new to codeigniter. 嗨,我是Codeigniter的新手。 I have installed codeigniter on Ubuntu 14.04 and I'm able to see the welcome page. 我已经在Ubuntu 14.04上安装了codeigniter,并且可以看到欢迎页面。 However other controllers that I create are not working. 但是,我创建的其他控制器无法正常工作。

I get a message the page not found. 我收到一条消息,找不到页面。 I have looked around a lot but could not find anything to fix my issue. 我到处走走了很多,但找不到任何可解决我问题的方法。

Can anyone kindly help? 任何人都可以帮忙吗?

class Login extends CI_Controller {

public function __construct() {
    parent::__construct();
    $this->load->helper('url');
    $this->load->library('email');
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->library('session');
    $this->load->model('login_model');
}

public function index($msg = NULL) {
    $data['msg'] = $msg;
    if ($this->session->userdata('validated') == false) {
        $this->load->view('templates/header_login');
        $this->load->view('login_view', $data);
        $this->load->view('templates/footer');
    }
    else {
        redirect('home', 'refresh');
    }
}

Routes.php Routes.php

$route['default_controller'] = 'login';

The default codeigniter page works completely fine. 默认的codeigniter页面工作正常。 But If I change the default controller with mine then I get a message that page not found 但是,如果我用我的默认控制器进行了更改,则会收到一条消息,提示找不到该页面

Whenever you install Codeigniter, 每当您安装Codeigniter时,

You can always access any controller, with http://sitename/index.php/controller/method/parameter . 您始终可以使用http://sitename/index.php/controller/method/parameter访问任何控制器。 You remove index.php from url with .htaccess file. 您可以从带有.htaccess文件的网址中删除index.php。

You can access your login page with http://sitename.com/index.php/login 您可以使用http://sitename.com/index.php/login访问登录页面

Sample .htaccess code: RewriteEngine on RewriteCond $1 !^(index\\.php|images|robots\\.txt) RewriteRule ^(.*)$ /index.php/$1 [L] 样本.htaccess代码: RewriteEngine on RewriteCond $1 !^(index\\.php|images|robots\\.txt) RewriteRule ^(.*)$ /index.php/$1 [L]

To access your custom controller all you have to do is use index.php in your URL. 要访问自定义控制器,您只需在URL中使用index.php Such as if your controller name is User the use this url http://localhost/your_project/index.php/user 例如,如果您的控制器名称为User则使用此URL http://localhost/your_project/index.php/user

and if you want to go any other method in User controller like create then use 如果您想在User控制器中User其他任何方法(例如create使用

http://localhost/your_project/index.php/user/create

But if you don't like to see index.php in your URL then you may place a file in your root directory name .htaccess and put followings in this file 但是,如果您不希望在URL中看到index.php ,则可以在根目录名称.htaccess放置一个文件,并在此文件中放置以下内容

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

Ref: More Details 参考: 更多详细信息

now your URL looks like http://localhost/your_project/user/create 现在您的网址看起来像http://localhost/your_project/user/create

Codeigniter works like this Codeigniter的工作方式如下

www.yourdomain.com/controller/function www.yourdomain.com/controller/function

if you add index function in your controller you can visit directly 如果您在控制器中添加索引功能,则可以直接访问

www.yourdomain.com/controller/ www.yourdomain.com/controller/

check that you added index function 检查您是否添加了索引功能

Please add .htaccess in your project root 请在您的项目根目录中添加.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /stock
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
  ErrorDocument 404 /index.php
</IfModule>

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

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