Verdouze
Verdouze

Reputation: 467

You have requested a non-existent service "Users_Users". Symfony3

When I try to access at my service, got this error :

You have requested a non-existent service "Users_Users".

But the namespace, the name of the class, name of the file, name of folder is ok. I have already copy paste the name to be sure.

Controller ->

<?php

namespace UsersBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use UsersBundle\Form\UsersForm;
use Symfony\Component\HttpFoundation\Request;

class UsersController extends Controller
{        
    public function UsersAction($Id)
    {
        $Users = $this->container->get('Users_Users')->ListeUsers($Id);
        return $this->render('UsersBundle:Users:users.html.twig', array('Users' => $Users));
    }

Service :

<?php

namespace UsersBundle\Services;

class Users
{  
    public function __construct($ldap,$Core_ConnexionsBDD,$MySql_ConnexionsBDD, $session)
    {
        $this->ldap = $ldap;
        $this->Core_ConnexionsBDD = $Core_ConnexionsBDD;
        $this->MySql_ConnexionsBDD = $MySql_ConnexionsBDD;
        $this->session = $session;
    }

    public function ListeUsers($Id)
    {  
        $lists = $this->MySql_ConnexionsBDD->getUsers();
        return $lists->Liste($Id); 
    }

}

Un the service file, no problem, it's a copy paste of the

get('Users_Users')

Where is the problem? I use the same method for my 4 other bundle :/

Upvotes: 1

Views: 3868

Answers (2)

lenybernard
lenybernard

Reputation: 2448

Can you please provide the service definition ? You know in a yaml or an xml file.

Then, please check the name of your service is really Users_Users (which is, in my mind a weird name).

If you named your service like the best practice encourages to do (with the Full Qualified Class Name (FQCN) -> Read):

#app/config/services.yml
services:
    UsersBundle\Services\Users:
        class: Acme\TwitterClient
        arguments: ["@someLdapServiceName", "@Another/Good/Named/Service", "@..."]

then your service will be able by its name:

<?php
    $this->get('UsersBundle\Services\Users');

From Symfony 2.8 and in Symfony v3.3 by default, a great new feature is the Service autowiring. You can use it to ease the service definition but you need to pay attention because since the 3.3 version, the default service auto-configuration comes with default private services:

services: _defaults: public: false

Please note also that in the next LTS 3.4 version and in 4.0, the services are private by default, no more need this configuration line.

So if you want to access your service directly from your container's get method, you'll need to add this to your Users_Users service definition:

services: Users_Users: ... public: true

Upvotes: 0

Verdouze
Verdouze

Reputation: 467

In app/config.yml missing import :

   - { resource: "@UsersBundle/Resources/config/services.yml" }

Content of UsersBundle/Resources/config/services.yml :

services:

Users_Users:
    class: UsersBundle\Services\Users
    arguments: ["@Core_ConnexionsBDD","@MySql_ConnexionsBDD","@session"]

Upvotes: 1

Related Questions