Search code examples
symfonyservicesymfony3

how make services in symfony3


i have this controller

namespace InicioBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use EntidadesBundle\Entity\Usuarios;
use Symfony\Component\HttpFoundation\Session\Session;
class DefaultController extends Controller
{
    private $session;

    public function __construct(){
        $this->session = new Session();
    }

   ..... 

        public function ver_rol($rol){
        if($this->sacarRol() === $rol){ 
                 return true;
             }else{
                 return false;
             }
    }

}

and in the services.yml , i got this:

parameters:
    #parameter_name: value

services:
    app.rolSession:
        class: InicioBundle\Controller\DefaultController
        arguments: ["i dont know how get paramets"]

the problem is that it doesnt work, symfony return an error FileLoaderLoadException, that the services.yml does not caontain valid YAML


Solution

  • There is a space before parameters: in your services.yml file, maybe remove that and your yaml should be valid.

    Also if you are passing no arguments to constructor you can just delete arguments: ["null"]

    One more thing, IIRC you need to add FQCN as class name, so class: InicioBundle\Controller\Default => class: InicioBundle\Controller\DefaultController

    While we are at the subject, you can type hint Request in your action and use it to getSession() or maybe inject @session service to your controller