Symfony restbundle argument for custom service controller non given - symfony

i want to implement service controller as rest controller which not extends from fosRestController, this mean i inject the service by my self. But i get always the error that:
Can't locate "api.contact_controller:postContactAction" controller in api.contact_controller:postContactAction (which is being imported from "/var/www/XXXX/XXXXX/XXXXX/app/config/routing.yml").
Service class:
api.contact_controller:
class: XXXX\ApiBundle\Controller\ContactController
parent: XXXX_rest_controller
arguments: [ "#templating"]
Then the routing:
contact:
type: rest
resource: 'api.contact_controller:postContactAction'
Finally the Controller
namespace XXXXX\ApiBundle\Controller;
use FOS\RestBundle\Routing\ClassResourceInterface;
use XXXXX\ContentBundle\Form\ContactFormType;
use XXXXX\Frontend\Controller\RestController;
use XXXX\Frontend\Model\Contact;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Class ContactController
*
* #package XXXXX\ApiBundle\Controller
*/
class ContactController extends RestController
{
/**
* #var EngineInterface $templating
*/
private $templating;
/**
* ContactController constructor.
* #param EngineInterface $templating
*/
public function __construct (EngineInterface $templating)
{
}
public function postContactAction (Request $request)
{
echo '<pre>';
print_r($request);die;
}
}
The Form:
<form id="contactform" method="post" action="{{ path('post_contact') }}" name="contactform">
.....
</form>

Now it works like below:
Routing definition
contact:
type: rest
resource: "api.contact_controller"
Without action in it!

Related

Symfony3 Use Entity Manager in Custom Container

I want to create HelperController for my project. I generate a controller with doctrine:generate:controller and I need to use entity manager in it.
I enjected to services.yml but it is giving an error like this:
Argument 1 passed to CampingBundle\Controller\HelperController::__construct() must be an instance of Doctrine\ORM\EntityManager, none given ...
My Controller Code :
namespace CampingBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Doctrine\ORM\EntityManager;
class HelperController extends Controller
{
protected $manager;
public function __construct(EntityManager $manager)
{
$this->manager = $manager;
}
My Services.yml :
services:
camping.helper_controller:
class: CampingBundle\Controller\HelperController
arguments: ["#doctrine.orm.entity_manager"]
Why it doesn't work ? Shoudl I clear cache or something else or is there anything wrong in definition ?
Thanks
Try to use EntityManagerInterface and remove extends Controller.
Check this link if you need CAS (Controllers as Services).
Change protected $manager; to private $manager;
namespace CampingBundle\Controller;
use Doctrine\ORM\EntityManagerInterface;
class HelperController
{
/**
* #var EntityManagerInterface $entityManager
*/
private $entityManager;
/**
* #param $entityManager
*/
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
}
I'll leave my two cents here as I had same issue and fixed it by adding tags to service.
something.validate.some_service:
class: Path\To\Some\Validator
arguments:
- '#doctrine.orm.entity_manager'
tags:
- { name: validator.constraint_validator, alias: some_validator_alias }
How to Work with Service Tags by Symfony

symfony adding repository function before rendering base.html.twig

In Symfony 3.4, base.html.twig I have a navbar showing number of the current user's messages. I use a repository entity function to do this. This function must be call every time when template base.html.twig is rendering but I don't want to put this function in all controllers how to do this by event listener before rendering base.html.twig? Override base controller ?
base.html.twig :
....
{{ include top_bar_nav.html.twig }}
....
A custom Twig extension is the correct way:
example in twig:
{{ number_of_current_users() }}
create twig extension like this:
<?php
namespace AppBundle\Twig;
use Doctrine\ORM\EntityRepository;
class UserExtension extends \Twig_Extension
{
/**
* #var EntityRepository
*/
private $userRepository;
/**
* #param EntityRepository $repository
*/
public function __construct(EntityRepository $repository)
{
$this->userRepository = $repository;
}
/**
* {#inheritdoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('number_of_current_users', array($this, 'numberOfCurrentUsers')),
);
}
/**
* #param $sku
*
* #return string
*/
public function numberOfCurrentUsers()
{
return $this->userRepository->getNumberOfCurrentUsers();
}
/**
* {#inheritdoc}
*/
public function getName()
{
return 'user';
}
}
and register it like this:
app.twig.users:
class: AppBundle\Twig\UserExtension
arguments: ['INJECT YOUR USER REPOSITORY HERE']
public: false
tags:
- { name: twig.extension }

symfony2 and godaddy route returns 404 response

I have uploaded to my godaddy webhost this cotroller in my symfony
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
class DefaultController extends Controller {
/**
* #Route("/contact")
*/
public function contactAction(Request $request) {
/* my contact action */
}
/**
* #Route("/", name="homepage")
*/
public function indexAction(Request $request) {
/* my index action */
}
}
the problem is with my contact action. when I go to www.myserver.net/contact it returns 404 response, but it loads normally the index action. What do I do wrong?
Make sure your app/config/routing.yml contains this:
app:
resource: "#AppBundle/Controller/"
type: annotation

Symfony2 - Controller as a Service - Routing via annotations

I am experimenting with creating controllers as services as shown at http://symfony.com/doc/current/cookbook/controller/service.html. I’ve followed this example and everything works fine when I have the route set in app/config/routing.yml. However when I try and set the route via annotations I get an error
My routing.yml file looks like this:
#hello:
# path: /hello/{name}
# defaults: { _controller: app.hello_controller:indexAction }
hello:
resource: "#EventBundle/Controller/HelloController.php"
type: annotation
My controller looks like this:
<?php
namespace Me\EventBundle\Controller;
//use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
//class HelloController extends Controller
class HelloController
{
private $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
/**
* #Route("/hello/{name}", name="hello")
*
*/
public function indexAction($name)
{
return $this->templating->renderResponse(
'EventBundle:Default:test.html.twig',
array('name' => $name)
);
}
}
As I say if I just use routing.yml and not the annotations the page renders correctly. However using annotations I get the error:
Catchable Fatal Error: Argument 1 passed to Me\EventBundle\Controller\HelloController::__construct() must be an instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface, none given, called in /Library/WebServer/Documents/symfony-project/app/cache/dev/classes.php on line 2176 and defined
EDIT - as requested in comments:
service.yml looks like:
services:
app.hello_controller:
class: Me\EventBundle\Controller\HelloController
arguments: ['#templating']
The answer with thanks especially to #Artamiel and #Cerad was to add #Route(service="app.hello_controller") just above my class name, so it now looks like:
/**
* #Route(service="app.hello_controller")
*/
class HelloController
{
private $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
..........etc

Inject service in FOSRest Controller

I am new at symfony and trying to create REST api using FOSRest Bundle. Also i want to use flysystem library as service in my controllers. It is simple as that
class FileController extends FOSRestController
{
public function getFilesAction()
{
$filesystem = $this->container->get('oneup_flysystem.application_filesystem');
...
}
}
This works fine, but my idea is to Inject this service into FileController so i can use $filesystem service in every method in my controller.
I read about it, that I have to make my controller as service, but then something went wrong. What is the proper way to make this injection.
We use something like this:
YourBundle/Resources/config/services.yml
controller.file:
class: YourBundle\Controller\FileController
arguments:
- #yourFileService
- #service_container
YourBundle/Controller/FileController.php
/**
* #Route("/file", service="controller.file")
*/
class FileController extends Controller
{
/**
* #var Filesystem
*/
private $filesystem;
/**
* #param Filesystem $filesystem
* #param $container
*/
public function __construct(
Filesystem $filesystem,
$container
) {
$this->filesystem = $filesystem;
$this->container = $container;
}

Resources