Dependency component issue - symfony

I'm new in Symfony and after I finished course "Create your own PHP Framework" (which is based on symfony components), I wanted to expand my framework by inject constructor of controller by model, but I got stuck. I got following error.
Error:
Catchable fatal error: Argument 1 passed to Controller\HelloController::__construct() must be an instance of Model\TestModel, none given, called in /var/www/html/vendor/symfony/http-kernel/Controller/ControllerResolver.php on line 195 and defined in /var/www/html/src/Controller/HelloController.php on line 21
HelloController constructor
public function __construct(TestModel $testModel)
{
$this->testModel = $testModel;
}
In container
....
$containerBuilder->register('model', \Model\TestModel::class);
$containerBuilder->register('hello', \Controller\HelloController::class)
->addArgument(new Reference('model'));
....
Routes
$routes->add('index', new Routing\Route('/hello/{name}',array(
'name' => 'World',
'_controller' => 'Controller\HelloController::index'
)));
I will be grateful for every advice.
Thanks

Related

phpunit testable livewire fails with Undefined array key "fingerprint"

When trying a test that came with Laravel and Jetstream/Livewire libraries, I get an undefined array key "fingerprint" error message
Undefined array key "fingerprint"
at vendor/livewire/livewire/src/Testing/TestableLivewire.php:181
public function pretendWereSendingAComponentUpdateRequest($message, $payload)
{
$result = $this->callEndpoint('POST', '/livewire/message/'.$this->componentName, [
'fingerprint' => $this->payload['fingerprint'],
'serverMemo' => $this->payload['serverMemo'],
'updates' => [['type' => $message, 'payload' => $payload]],
]);
This happens for any out of the box feature tests that ship with Laravel9 with Jetstream when used against my project.
Here is one example that fails at the Livewire::test.... line.
The user is created and authenticating without issue and confirmed in other phpunit tests.
class BrowserSessionsTest extends TestCase
{
use RefreshDatabase;
public function test_other_browser_sessions_can_be_logged_out(): void
{
$this->actingAs($user = User::factory()->create());
Livewire::test(LogoutOtherBrowserSessionsForm::class)
->set('password', $user->password)
->call('logoutOtherBrowserSessions')
->assertSuccessful();
}
}
I stood up a fresh Laravel 9 project which works and began inserting various areas from my project into the fresh project as a way of hopefully identifying the issue. Session parameters, events, migrations, factories, models, were not the issue as it continued to work in the fresh project.
One thing I noticed is that the generic routes are not accepted in my project within he test cases. I have to insert 'https://realtor.host' in front of every test route (e.g. $response = $this->get('https://realtor.host/register');
I was curious if it was not evaluating the livewire route and I tried to add my domain into the vendor's livewire component in which the test still failed and that did not cause it to work.
Any ideas on where else I can look?

Cannot autowire argument $article : Symfony 5

I'am currenty face to this error when i try to get my article page : Cannot autowire argument $article of "App\Controller\HomeController::show()": it references class "App\Entity\Article" but no such service exists.
I just created a very new small symfony 5 application :
The code in my controller is :
/**
* #Route("/show/{id}", name="show")
*/
public function show(Article $article): Response
{
if(!$article) {
return $this->redirectToRoute('home');
}
return $this->render('show/index.html.twig', [
'article' => $article,
]);
}
I'am following a tutorial on Udemy and i guess that i did the same thing than the former. Maybe version difference ?
Thanks in advance for your helps.
Thierry
The error you have is saying that it cannot find the auto-wire path to your entity. Using the Symfony installer will automatically configure parts like this for you.
The main part which auto-wires the Entity to the Dependency Injection layer is through Doctrine configuration and the Symfony ParamConverter found as part of the SensioFrameworkExtraBundle.
The ParamConverter has a lot of options to be able to convert parameters inside of the #Route into an Entity. Using your example above we see that Symfony will internally call ArticleRepository->findOneById($id) and return the result to your controller.
EDIT: Removed all mentions of Doctrine configurations. The errors that are displayed when Doctrine is not auto-wiring are different.
Same problem here (with attributs instead of annotations) on Symfony 5
I have even tried to explicite ParamConverter and Entity:
#[Route('/article/{id}/editer', name: 'edit_article', requirements: ["id" => "\d+"], methods: ['GET','POST'])]
#[ParamConverter('id', options: ['mapping' => ['id' => 'id']])]
#[Entity('article', expr: 'repository.findOneBySlug(id)')]
public function editArticle(Article $article, Request $request,
CategoryRepository $categoryRepository, EntityManagerInterface $manager): Response
{
dump($article); die;
}
but i have got the error message:
Cannot autowire argument $id of "App\Controller\DefaultController::editArticle()":
it references class "App\Entity\Article" but no such service exists.
Although there is:
use App\Entity\Article;
And the solution like #nico-haase said is to install this bundle
composer require sensio/framework-extra-bundle

Option Model_manager for ModelAutocompleteType::class in SonataAdminBundle

i am trying to upgrade my application to symfony 3. But there a deprecation notice i can't fix:
My Code with Symfony 2 :
protected function configureFormFields(FormMapper $formMapper){
->add('town', 'sonata_type_model_autocomplete', array(
'property'=>'name',
'placeholder'=>'Select one',
'label'=>'town.name,
));
}
The deprecation notice :
Accessing type "sonata_type_model_autocomplete" by its string name is
deprecated since version 2.8 and will be removed in 3.0. Use the
fully-qualified type class name
"Sonata\AdminBundle\Form\Type\ModelAutocompleteType" instead
So, to be compliant with Symfony 3 :
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
protected function configureFormFields(FormMapper $formMapper){
->add('town', ModelAutocompleteType::class , array(
'property'=>'name',
'placeholder'=>'Select one',
'label'=>'town.name',
'model_manager'=> null
));
}
But i get this error :
Catchable Fatal Error: Argument 1 passed to
Sonata\AdminBundle\Form\DataTransformer\ModelToIdPropertyTransformer::__construct()
must implement interface
Sonata\AdminBundle\Model\ModelManagerInterface, null given, called in
/vendor/sonata-project/admin-bundle/Form/Type/ModelAutocompleteType.php
on line 37 and defined
So instead of null, i have to pass an ModelManagerInterface object, but where i can find it ?
Thank you for reading

Symfony 2.5.6 error InvalidArgumentException: The service definition "event_dispatcher" does not exist

I'm trying to build my first Compiler Pass in Symfony 2. For now, I'm just trying to get the core event_dispatcher service from FrameWorkBundle inside a SampleBundle, but I get this error :
error InvalidArgumentException: The service definition "event_dispatcher" does not exist.
Here is the code for my compiler :
<?php
namespace Me\SampleBunlde\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class RegisterListenersPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definition = $container->getDefinition('event_dispatcher');
}
}
?>
I'm a bit surprised since I'm following step by step a professionnal Symfony book who assures me that I will find this service with that id.
I've done some researches about that, and I discovered that only the debug.event_dispatcher service was avaible. Then I checked for aliases and saw that there was a private Alias named 'event_dispatcher' pointing to debug.event_dispatcher. So I'm really confused about all that. And I'm wondering :
Why is the Alias private ? Do I need to set him Public or is it the wrong way ?
Why Symfony does not automatically interprets my event_dispatcher call ?
Thank you for your help !
Use findDefinition() instead of getDefinition(). findDefinition also looks for aliases.

get entity from user name FosuserBundle Symfony

i have entity Collab that extend from Fos\UserBundle\Model\User, i want to get the entity from the username that i login .
i my controller :
echo $this->getUser();
$collab->get('collab.collaborateurservice')->findCollaborateurByUserName($this->getUser());
echo $this->getEmailCollaborateur();
in my manager i defined i method :
public function findCollaborateurByUserName ($username){
return $this->getRepository()->findOneBy(array('username'=>$username));
}
i get this exception :
FatalErrorException: Error: Call to a member function get() on a non-object in
how can get this entity??
Your problem is in this lines:
return $this->getRepository()->findOneBy(array('username'=>$username));
$collab->get('collab.collaborateurservice')->findCollaborateurByUserName($this->getUser());
It should be:
return $this->getDoctrine()->getRepository('YourBundleNamespace:Collab')->findOneBy(array('username'=>$username));
$collab = $this->get('collab.collaborateurservice')->findCollaborateurByUserName($this->getUser()->getUsername());
What is $collab? You're getting Fatal Error trying to call get method on variable that was not properly initialized (non-object).

Resources