CodeIgniter控制器模型404错误

时间:2015-08-17 21:37:11

标签: php .htaccess codeigniter

我在CodeIgniter网站上收到404错误,我已经查看了其他答案并使用了.htaccess但尚未解决。它位于我的domain.co.uk/admin

控制器 -

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Projects extends CI_Controller {

public function __construct()
{
    parent::__construct();
}

public function index() {
    $this->load->model('project');
    $data['projects']=$this->project->get_projects();
    echo"<pre>";print_r($data['projects']);echo"</pre>";
}
}

模型 -

<?php

class Project extends CI_Model{

function get_projects($num=10,$start=0){
    $this->$db->select()->from('projects')->where('active',1)->order_by('date_added','desc')->limit($num,$start);
    $query=$this->db->get();
    return $query->result_array();
}
}

我还添加了这些路线 -

$route['default_controller'] = 'projects';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

目前在我的域/管理文件夹中有.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

任何人都可以看到为什么我可能会收到404错误

3 个答案:

答案 0 :(得分:0)

您对/*! * classie - class helper functions * classie.has( elem, 'my-class' ) -> true/false * classie.add( elem, 'my-new-class' ) * classie.remove( elem, 'my-unwanted-class' ) * classie.toggle( elem, 'my-class' ) */ /*jshint browser: true, strict: true, undef: true */ /*global define: false */ (function (window) { 'use strict'; // class helper functions from bonzo https://github.com/ded/bonzo function classReg(className) { return new RegExp("(^|\\s+)" + className + "(\\s+|$)"); } // classList support for class management // altho to be fair, the api sucks because it won't accept multiple classes at once var hasClass, addClass, removeClass; if ('classList' in document.documentElement) { hasClass = function (elem, c) { return elem.classList.contains(c); }; addClass = function (elem, c) { elem.classList.add(c); }; removeClass = function (elem, c) { elem.classList.remove(c); }; } else { hasClass = function (elem, c) { return classReg(c).test(elem.className); }; addClass = function (elem, c) { if (!hasClass(elem, c)) { elem.className = elem.className + ' ' + c; } }; removeClass = function (elem, c) { elem.className = elem.className.replace(classReg(c), ' '); }; } function toggleClass(elem, c) { var fn = hasClass(elem, c) ? removeClass : addClass; fn(elem, c); } var classie = { // full names hasClass: hasClass, addClass: addClass, removeClass: removeClass, toggleClass: toggleClass, // short names has: hasClass, add: addClass, remove: removeClass, toggle: toggleClass }; // transport if (typeof define === 'function' && define.amd) { // AMD define(classie); } else { // browser global window.classie = classie; } })(window); /** * main.js * http://www.codrops.com * * Licensed under the MIT license. * http://www.opensource.org/licenses/mit-license.php * * Copyright 2014, Codrops * http://www.codrops.com */ (function () { var bodyEl = document.body, content = document.querySelector('.content-wrap'), openbtn = $(".menu-button").click(function () { $(this).next(".menu-wrap").toggleClass("show-menu"); }), closebtn = document.getElementById('close-button'), isOpen = false; function init() { initEvents(); } function initEvents() { for (var i=0; i< openbtn.length ; i++){ openbtn[i].addEventListener('click', toggleMenu) } if (closebtn) { closebtn.addEventListener('click', toggleMenu); } // close the menu element if the target it´s not the menu element or one of its descendants.. if (content){ content.addEventListener('click', function (ev) { var target = ev.target; if (isOpen && target !== openbtn) { toggleMenu(); } }); } } function toggleMenu() { if (isOpen) { classie.remove(bodyEl, 'show-menu'); } else { classie.add(bodyEl, 'show-menu'); } isOpen = !isOpen; } init(); })(); 变量有什么看法? (它应该在\ application \ config中定义。)

如果您有$route['default_controller'],则您的索引网址应为$route['default_controller'] = "Projects"domain/admin/index.php/projects *

*您可以使用带有一些简单规则的.htaccess文件轻松删除此文件。下面是这样一个文件的示例,使用“否定”方法,除了指定的项目之外,所有内容都被重定向:

domain/admin/projects

答案 1 :(得分:0)

在codeigniter 3上,你应该始终将你的类的第一个字母和文件名作为大写的模型和控制器和库。

此外,我会将项目模型重命名为Model_project.php,因为如果名称相同,codeigniter可能会混淆。

模型 Model_project.php

class Model_project extends CI_Model {

}

控制器 Project.php

class Project extends CI_Controller {
    public function __construct() {
       parent::__construct();
       $this->load->model('model_project');
    }
}

您可能需要设置路线

http://www.codeigniter.com/user_guide/general/routing.html

答案 2 :(得分:0)

如果您使用的是codeigniter 3.0,请以大写字母开头更新您的MODEL和CONTROLLER,并且不要像上面的代码中那样为模型和控制器指定相同的名称。 例如:

class Projects extends CI_Controller {
   //code comes here
}

在此项目中,名称绝对正常,但请注明您的文件名也应以大写字母开头,即Project.php 模特的另一个:

class Projects_model extends CI_Model {
       //code comes here
    }

这是一种正确使用codeigniter的方法,而且我已经告诉过你,MODEL和CONTROLLER文件名必须以大写字母开头。

谢谢..希望它能帮到你