简体   繁体   English

无法在Laravel 4框架的控制器中的构造方法中加载模型

[英]Not able to load a model in the construct method in the controller of the Laravel 4 framework

Here is the beginning of my controller code: 这是我的控制器代码的开头:

<?php

class AppController extends BaseController {

    /**
     * App Model
     * @var app
     */
    protected $app;

    /**
     * User Model
     * @var User
     */
    protected $user;

    /**
     * Inject the models.
     * @param app $app
     * @param User $user
     */
    public function __construct(App $app, User $user)
    {
        parent::__construct();
        $this->app = $app;
        $this->user = $user;
    }

In my models folder I have a file called App.php 在我的模型文件夹中,我有一个名为App.php的文件

Here is the beginning of the code: 这是代码的开头:

<?php

    use Illuminate\Support\Facades\URL; # not sure why i need this here :c
    use Robbo\Presenter\PresentableInterface;

    class App extends Eloquent implements PresentableInterface {

Now I did a var_dump() of the $this->app var and I got this: 现在我做了$ this-> app var的var_dump(),我得到了:

object(Illuminate\Support\Facades\App)#472 (0) { }

I am not sure why it's trying to get a Facades when It's supposed to get my model. 我不确定为什么要建立模型时要尝试创建Facades。

Also here is a part of my routes file: 这也是我的路线文件的一部分:

/** ------------------------------------------
 *  Route model binding
 *  ------------------------------------------
 */
Route::model('user', 'User');
Route::model('app', 'App');
Route::model('role', 'Role');

# Apps - Second to last set, match slug
Route::get('{appSlug}', 'AppController@getView');
Route::post('{appSlug}', 'AppController@postView');

# Index Page - Last route, no matches
Route::get('/', 'AppController@getIndex');

I assume that the rest is irrelevant. 我认为其余的都是无关紧要的。 But if not, do please ask for the rest. 但是,如果没有,请其余部分。

This is because of the IoC container. 这是因为有IoC容器。 App is already taken by the main application. 主应用程序已使用该应用程序。 What you could do is add an alias in app/config/app.php 您可以做的是在app / config / app.php中添加一个别名

'aliases' => array(

    ... current aliases,

    'AppModel' => 'Robbo\Models\App'
);

Of course the App model needs to be namespaced to Robbo\\Models in this case. 当然,在这种情况下,App模型需要命名为Robbo \\ Models。

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

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