简体   繁体   中英

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

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:

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.

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. App is already taken by the main application. What you could do is add an alias in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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