简体   繁体   English

找不到Kohana视图模型类

[英]Kohana View Model Class Not Found

I am stumped, I must be missing something basic, any ideas would be much appreciated. 我很沮丧,我必须缺少一些基本的知识,任何想法都将不胜感激。

I have setup a new Kohana project which works fine with Models and Controllers. 我已经建立了一个新的Kohana项目,该项目可以很好地与Models和Controllers配合使用。 For this example I have stripped it right back to a single very basic Model for a User and a single Controller with a single index action inside it. 对于此示例,我将其剥离为一个用户的一个非常基本的模型和一个内部具有单个索引动作的单个控制器。

I decided to use KOstache as my template engine as I heard good things about it. 当我听到有关它的好消息时,我决定将KOstache用作模板引擎。 I downloaded the module and the vendor submodule and this does seem to work fine. 我下载了模块和供应商子模块,这似乎工作正常。

My problem arrises when trying to create a new instance of my view model class named View_User, kohana throws an * ErrorException [ Fatal Error ]: Class 'View_User' not found * 尝试创建名为View_User的视图模型类的新实例时,我的问题浮出水面,kohana抛出* ErrorException [致命错误]:未找到类'View_User'*

My directory structure is as follows 我的目录结构如下

application
   |_classes
   |     |_Controller
   |     |   |_User.php
   |     |_Model
   |     |   |_User.php
   |     |_view
   |         |_user.php
   |_templates
         |_user.mustache

There are other folders within the project but I believe these are the relevan ones. 项目中还有其他文件夹,但我相信这些是相对的。

My controller seems to be the class with the problem 我的控制器似乎是有问题的班级

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_User extends Controller {

        public function action_index()
        {
                $renderer = Kostache_Layout::factory();
                $view = new View_User;
                $view->title = 'This is the test title';
                $this->response->body($renderer->render($view));
        }

}

This does not work and complains that it cannot find class View_User yet in my classes/view/user.php file I clearly have a View_User class 这不起作用,并且抱怨它在我的class / view / user.php文件中找不到类View_User,我显然有一个View_User类

<?php
class View_User {
}

Now I assume it is some sort of problem with the way I am setting up KOstache or Kohana, but I am unsure what I am doing wrong. 现在,我假设我设置KOstache或Kohana的方式存在某种问题,但是我不确定自己做错了什么。

If I include the class definition at the bottom of the classes/Controller/User.php then everything works as expected, it just doesn't find the class within another file. 如果我在class / Controller / User.php的底部添加了类定义,那么一切都会按预期进行,只是在另一个文件中找不到该类。

From what I've read if the autoloader tries to load class View_User it will look in classes/view/user.php 从我读过的内容来看,如果自动加载器尝试加载View_User类,它将在classes / view / user.php中查找

What am I doing wrong? 我究竟做错了什么?

Sure, the issue its with Folders and File names. 当然,与文件夹和文件名有关。

From Kohana v3.3 Doc: "Class names should have uppercase first letters with underscores to separate words. Underscores are significant as they directly reflect the file location in the filesystem." 来自Kohana v3.3 Doc:“类名应使用大写的首字母,并用下划线分隔单词。下划线很重要,因为它们直接反映了文件系统中的文件位置。”

classes/view/user.php should become classes/View/User.php classes / view / user.php应该变成classes / View / User.php

Reference: http://kohanaframework.org/3.3/guide/kohana/conventions#class-names-and-file-location 参考: http : //kohanaframework.org/3.3/guide/kohana/conventions#class-names-and-file-location

Views folder shouldn't be under classes, but should be like this: Views文件夹不应位于类下,而应如下所示:

application
 |_classes
 |     |_Controller
 |     |   |_User.php
 |     |_Model
 |     |   |_User.php
 |_views
 |  |_user.php
 |_templates
    |_user.mustache

You can look here http://kohanaframework.org/3.0/guide/kohana/files for reference. 您可以在这里http://kohanaframework.org/3.0/guide/kohana/files进行参考。

It turns out that it was a case issue on my folders and filenames. 原来,这是我的文件夹和文件名出现问题的情况。

The classes/view/user.php should have been class / view / user.php应该是

classes/View/User.php for classes I named View_User 我名为View_User的类的classes / View / User.php

After changing this it all works as expected. 更改此设置后,所有操作均按预期进行。

You might have a permission problem. 您可能有权限问题。 In my case it was the httpd service (apache2) that couldn't access my web-project files. 就我而言,是httpd服务(apache2)无法访问我的Web项目文件。 Here is a brutal solution (do it only if your security situation allows it): 这是一个残酷的解决方案(仅在您的安全情况允许的情况下才这样做):

$ sudo chmod -R 0777 /var/www/html/mysite/

More info: move_uploaded_file gives "failed to open stream: Permission denied " error after all configurations i did 更多信息: 在完成所有配置后,move_uploaded_file给出“无法打开流:权限被拒绝”错误

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

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