1
votes

I'm using latest (dev-master) sonata admin and I want to create my own createAction() method for sonata admin. I have to do that, because I want to save some user information, while adding to database.

My custom controller is - S\CoreBundle\Controller\NewsAdminConroller.php

<?php
namespace S\CoreBundle\Controller;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Symfony\Component\Security\Core\SecurityContextInterface;


class NewsAdminController extends Controller
{
    /**
    * Set the system user ID
    */
    private function updateFields($object)
    {
        //some code - this is my own method
    }

    public function createAction(Request $request = null)
    {
        //code for create ... it's almost the same as default code. 
    }
}

Default CRUD - Sonata\AdminBundle\Controller\CRUDController.php:

class CRUDController extends Controller
{
    public function createAction(Request $request = null)
    {
         //...
    }
}

Both createAction() methods have exactly the same arguments, name ...

And it throw's me an error:

PHP Strict Standards: Declaration of S\CoreBundle\Controller\NewsAdminController::createAction() should be compatible with Sonata\AdminBundle\Controller\CRUDController::createAction(Symfony\Component\HttpFoundation\Request $request = NULL) in /home/mark/dev/project/src/S/CoreBundle/Controller/NewsAdminController.php on line 129

1
Can you include "use Symfony\Component\HttpFoundation\Request;" in your controller ? - BENARD Patrick
I agree with @YenneInfo - it looks like you might need to add a use statement for the Request - it is likely PHP is assuming it is from the same namespace as your CRUDController and so the declaration of your function is incompatible with the Sonata one. - madebydavid
Thanks. That was the trick. You can post the answer and I will accept it. - repincln

1 Answers

2
votes

The Sonata\AdminBundle\Controller\CRUDController::createAction(Symfony\Component\HttpFoundation\Request $request = NULL)

Needs a Request Object, but if you don't declare it, it point to S\CoreBundle\Controller\Request

Just add "use Symfony\Component\HttpFoundation\Request;" in top of file.

Update

Since the commit https://github.com/sonata-project/SonataAdminBundle/commit/49557c302346f57d962b83b31e2931446ff60e9c, there is no need to set the request as parameter.

The create Action is only

Sonata\AdminBundle\Controller\CRUDController::createAction()