Entity object not found by the #ParamConverter annotation - symfony

I'm getting this error in a controller that was previously working.
I have added some new Controllers (with different route prefix) so I guess the error might come from those new routes generated, but, honestly, I'm quite puzzled with how this can error could appear in a controller that has been long time unaltered.
This is my controller code:
ContractController.php
/**
* #Route("/contract")
*/
class ContractController extends AbstractController
{
/**
* #Route("/edit/{id}", name="contract_edit", methods={"GET","POST"})
*/
public function edit(Request $request, DocumentManagerService $dm, EventDispatcherInterface $dispatcher, Contract $contract): Response
{
}
I get this error when accesing /contract/edit/1234:
App\Entity\Contract object not found by the #ParamConverter annotation.
If I add a #ParamConverter annotation, like this:
* #ParamConverter("contract", options={"mapping": {"id" : "id"}})
I get the same error. This same error occurs with other routes in the same controller, as I say all of them were working previously.
If I debug:router in console, I get hits routed prefixed with /contract, all of them are in this controller.
contract_debug GET ANY ANY /contract/debugcontract/show/{id}
contract_debug_edit GET|POST ANY ANY /contract/debugcontract/edit/{id}
contract_debug_renew GET|POST ANY ANY /contract/debugcontract/renew/{id}
contract_index GET ANY ANY /contract/list
contract_new GET|POST ANY ANY /contract/new/{client_id}
contract_edit GET|POST ANY ANY /contract/edit/{id}
contract_add_payment GET|POST ANY ANY /contract/addpayment/{id}/{renew}/{cardid}
contract_payment_delete GET ANY ANY /contract/deletepayment/{id}
contract_show GET ANY ANY /contract/show/{id}
contract_send_and_block GET ANY ANY /contract/send/{id}
contract_protect GET ANY ANY /contract/protect/{id}/{protect}
contract_generate_invoice GET ANY ANY /contract/invoice/{id}/{send}

I found the error, it had nothing to do with routes.
I created a new ContractRepository class for this entity, and copied/pasted the base code, resulting in this:
class ContractRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Client::class);
}
}
I was sending another entity class to the constructor, it should be Contract, not Client.

Related

(Symfony 4) How can I get the base URI of my project (the http://www.yourwebsite.com/) from within a non-controller class?

How can I get the "http://www.yourwebsite.com" from my repository class in Symfony 4?
The reason why I need to do this is because I'm using the Liip image service which returns the entire url, and I only need the url relative to the root, so I have to strip out the "http://www.yourwebsite.com" from the path returned.
I have used KernelInterface and that only returns the path from within your machine (i.e. the var/www/... in your machine).
I've tried injecting http foundation's Request object so I can call the getPathInfo() method, here is what I have in my repository class:
use Symfony\Component\HttpFoundation\Request;
class PhotoRepository extends ServiceEntityRepository
{
/**
* #var Request
*/
protected $request;
public function __construct(Request $request){
$this->request = $request;
}
But I just get the error Cannot autowire service "App\Repository\PhotoRepository": argument "$request" of method "__construct()" references class "Symfony\Component\HttpFoundation\Request" but no such service exists.
Here is what I have under "services" in my services.yaml:
App\Repository\PhotoRepository:
arguments:
- Symfony\Component\HttpFoundation\Request
Here is the full path to my file generated:
"http://www.mywebsite.com/media/cache/my_thumb/tmp/phpNbEjUt"
I need to parse out the get the http://www.mywebsite.com and just get the /media/cache/my_thumb/tmp/phpNbEjUt from the path.
As Cerad already wrote in the comments, you can inject the Symfony\Component\HttpFoundation\RequestStack:
App\Repository\PhotoRepository:
arguments:
- Symfony\Component\HttpFoundation\RequestStack
- Doctrine\Common\Persistence\ManagerRegistry
Your constructor for the PhotoRepository will then look something like:
class PhotoRepository extends ServiceEntityRepository
{
/**
* #var RequestStack
*/
protected $requestStack;
public function __construct(RequestStack $requestStack, ManagerRegistry $managerRegistry)
{
parent::__construct($managerRegistry, Photo::class);
$this->requestStack = $requestStack;
}
...
}
You can then determine the current URL using something like this:
private function getCurrentUrl(): string
{
$request = $this->requestStack->getCurrentRequest();
return $request->getBaseUrl(); // or possibly getUri()
}

Symfony FOSUserBundle error

So, I've made an app with its own controller and entities. I've just installed the FOSUserBundle following the guide and I want to connect my app to the login, register, etc. I tried using the function 'path'. This is what looks like... 'href="{{path(menu.url)}}' ok, so I get an error: 'such route does not exist'. I decided to create another controller, this one called FosController, next to my previous one the DefaultController, I extended the FosController from SecurityController(login fosuserbundle) and decided to create with annotation a route called login and connect it to the parent method...
class FosController extends SecurityController
{
/**
* #Route("/hospitallogin", name="login")
* #Method({"GET", "POST"})
*
* #param Request $request
*
* #return Response
*/
public function login2Action(Request $request) {
parent::loginAction($request);
}
}
And I get this error:
The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?
I dont know what else to do, thanks for your time.
I decided to use this though it's not correct...
public function indexAction()
{
return $this->redirect('http://localhost:8000/login');
}

Symfony2 routing from POST

I'm issuing a POST body of: controller=user&action=add
To the URL: my_site.com/
I want acoording to post run addAction from UserController
class UserController extends Controller
{
public function addAction()
{
//return $this->redirectToRoute('test');
return $this->render('BeerwerdClaberonBundle:Default:index.html.twig');
}
}
How can I do this?
You can specify a route and with that route you can specify a request method.
For example:
/**
* #Route("/contact")
* #Method("GET")
*/
For more info see the documentation on routing:
http://symfony.com/doc/current/book/routing.html#adding-http-method-requirements

Symfon2 SensioFrameworkExtraBundle #Template annotation not working

Hi Have followed the instructions to enable the SensioFrameworkExtraBundle here: http://symfony.com/doc/2.1/bundles/SensioFrameworkExtraBundle/index.html
Thereafter I create the below controller:
namespace Acme\DemoBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class MyController
{
/**
* #Template
*/
public function indexAction()
{
}
}
If set up a route pointing to the indexAction on this controller and browse to it, I get the following error:
The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?
It looks like the SensioFrameworkExtraBundle is not actually enabled, but I cannot figure out why. I'm looking for advice.
The #Template annotation is working. As the error suggest, you must return something. If return array, it will be sent to template engine. Make sure the template exists.
public function indexAction()
{
...
$somedata = 'fill data';
return array('somedata' => $somedata);
}

Route is incorrectly associated with redirect controller

I'm attempting to design a RESTful interface in Symfony which will be called through AJAX requests. I've been having a problem where my POST method route is being matched to the built-in redirect controller instead of the one I created below:
/**
* #Route("/todos")
*/
class TodoController extends Controller
{
/**
* #Route("/", name="todos")
* #Method("GET")
*/
public function indexAction()
{
// Get action here
}
/**
* #Route("/{id}", name="todo_delete")
* #Method("DELETE")
*/
public function deleteAction($id)
{
// Delete action here
}
/**
* #Route("/", name="todo_create")
* #Method({"POST"})
*/
public function createAction()
{
return new Response("Hello!");
}
}
My indexAction and deleteAction work fine, but my createAction did not. When I looked at the logs this is what I saw:
[2011-10-24 19:27:14] request.INFO: Matched route "todo_create" (parameters: "_controller": "Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction", "path": "/todos/", "permanent": "true", "scheme": "null", "httpPort": "80", "httpsPort": "443", "_route": "todo_create") [] []
It looks like my route is not even being associated with the TodoController I created. Any idea what is causing this?
---> #Method({"POST"})
Shouldn't that be
#Method("POST")
?
I figured it out. It turns out my client side code was calling "http://todos.localhost/todos" where the routes were expecting "http://todos.localhost/todos/" (which has a trailing /). I removed the slash in my POST request route as follows
/**
* #Route("", name="todo_create")
* #Method({"POST"})
*/
and everything works fine.
It seems like Symfony realized that the slash was missing, added it to the request url, and performed an internal redirect on using the new url. When Symfony performed the redirect, however, it wasn't maintaining the request method (POST in this case). Instead it was calling my GET controller.

Resources