0

I'm using the Laravel framework and have a directory of the following structure:

models 
  Statistic.php

presenters
  StatisticPresenter.php

In my models/Statistic class, I'm trying to access a present method which calls a protected property which then references my presenter:

protected $presenter = 'presenters\StatisticPresenter';

public function present() {
    return new $this->presenter($this); 
}

Yet, this returns the error: Class 'presenters\StatisticPresenter' not found. I realize this is a namespacing error of some sort, and I've tried watching a few videos on how it works, but I simply can't wrap my head around it. I have already added my presenter folder to my composer.json file. For example, adding this to the top of my Statistic model does not work:

use presenters\StatisticPresenter;

How can I fix this?

1
  • If you haven't already, run composer dump-autoload. You can also try adding a backslash in at the beginning: '\presenters\StatisticPresenter' Commented Jan 17, 2015 at 9:32

1 Answer 1

2

Do the followings;

  • Mark your namespace in StatisticPresenter.php ? (at the top of file "namespace presenters;")
  • Add PSR-4 class map to your composer

{ "autoload": { "psr-4": { "presenters\\": "app/presenters/" } } }

  • run "composer dump-autoload" once and you wont need to run this command again for the "presenters" namespace if you add new classes into "app/presenters/ folder"
  • Test your class with "use presenters/StatisticPresenter;"
  • If you can access your class you dont need to change your code your present() function will be valid
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.