0

I am creating a webapp that has some common functions. So I figured the easiest way to do this would be to make a base controller and just extend that. So in the base controller I have (similar to):

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class BaseController extends Controller
{
    protected function dosomething($data)
    {
        return $data;
    }
}

And then in the default controller:

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends BaseController
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction()
    {
        $data = "OK";
        $thedata = $this->dosomething($data);
    }
}

And then for the Admin Controller: namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class AdminController extends BaseController
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction()
    {
        $data = "OK";
        $thedata = $this->dosomething($data);
    }
}

However, I am getting errors like "Compile Error: Access level to AppBundle\Controller\AdminController::dosomething() must be protected (as in class AppBundle\Controller\BaseController) or weaker", not just when I load the admin controller function, but default as well. When I stop the admin controller extending base controller, this error goes (seems to work on default but not admin).

I'm guessing somewhere I have to let Symfony know that the admin controller is safe or something?

3
  • 2
    Please add the code for AdminController. Currently it's just DefaultController again. But besides that, the error message is quite clear. It seems you have dosomething on AdminController as private. Commented Oct 13, 2016 at 6:17
  • Extending Symfony's controller class just is a simple way to get rid of a lot of boilerplate code, any callable can basically handle a request. Please don't extend the controller, but define a service with doSomething as a public method and call that from the controller. Commented Oct 13, 2016 at 11:08
  • It ended up being some code in AdminController that I missed (It was late, I was tired). Problem solved. Commented Oct 13, 2016 at 21:26

1 Answer 1

4

It has nothing to do with Symfony, it's PHP.

Obviously, you're trying to redefine dosomething method in your Admin Controller, and trying to make this method private.

It's not allowed. It may be either protected or public.

It's principle of OOP. Because if you would have a class SubAdminController, then instance of it would be also instance of both AdminController and BaseController. And PHP must definitely know if the method dosomething from parent class is accessible from SubAdminController.

Sign up to request clarification or add additional context in comments.

4 Comments

Im not re-defining it, it is defined in BaseController and then used in both DefaultController and AdminController. Only 1 of those controllers would be called on any page load (as they are 2 different sections of the site) so its not being declared twice. Also, dosomething is declared as protected (and I have tried declaring it as public with similar results) so that should not be an issue.
Excuse me, but I think you just missed something. Check it twice. Maybe, try to clear cache. The error message is quite clear - the problem is in re-defining of dosomething inside AdminController
Yeh, I saw that I missed some code in AdminController that re-declared it. Removed it and now it all works fine. Was trying to work out how to edit my comment but for some reason stack overflow wont let me edit or delete it! Your answer was correct and I am upvoting it.
I'm glad you've figured it out

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.