【问题标题】:Namespacing not working in Laravel命名空间在 Laravel 中不起作用
【发布时间】:2015-02-22 13:36:30
【问题描述】:

您好,我确实有一个名为“事件”的模型

/app/models/DC/Events/Event.php

<?php
namespace models\DC\Events;

class Event extends \Eloquent {

    protected $table = 'Events';

    protected $guarded = array('id', 'password_confirmation');
    protected $fillable = [
        'name', 
        'adress', 
        'zipcode', 
        'time', 
        'date', 
        'Fee', 
        'link', 
        'Description', 
        'country', 
        'city', 
        'fblink', 
        'lineup'
        ];
}

/app/controllers/EventController.php

<?php

use \models\DC\Events\Event as S;

class EventController extends \BaseController {


    /**
     * Display a listing of the resource.
     * GET /events
     *
     * @return Response
     */
    public function index()
    {
        $events = S::all();
        $events = S::paginate(5);
        return View::make('events.index');
    }
}

但它告诉我“找不到类 'models\DC\Events\Event'”

有什么解决建议或有更好的方法吗?

【问题讨论】:

  • 尝试在你的 use 语句中去掉前面的反斜杠。

标签: php class laravel namespaces


【解决方案1】:

为了更新自动加载文件以确保您的类都被包含在内,您需要运行

composer dump-autoload

【讨论】:

  • 我完全忘记了!这成功了,虽然它返回 Undefined variable: events 而 $events 被分配给 S::all();
  • 看起来您忘记将变量传递给视图。 return View::make('events.index', array('events' =&gt; $events'))
  • 你先生,让我开心!不知道需要传递视图中的变量。自从我们在模型中声明它以来,认为视图已经知道该变量。
猜你喜欢
  • 2014-03-26
  • 1970-01-01
  • 2019-06-14
  • 2015-04-03
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 1970-01-01
相关资源
最近更新 更多