如何在 symfony3 中制作服务
how make services in symfony3
我有这个控制器
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;
}
}
}
在 services.yml 中,我得到了这个:
parameters:
#parameter_name: value
services:
app.rolSession:
class: InicioBundle\Controller\DefaultController
arguments: ["i dont know how get paramets"]
问题是它不起作用,symfony return 一个错误 FileLoaderLoadException,services.yml 不包含有效的 YAML
在你的 services.yml
文件中 parameters:
之前有一个 space,也许删除它并且你的 yaml 应该是有效的。
此外,如果您不向构造函数传递任何参数,您只需删除 arguments: ["null"]
还有一点,IIRC 您需要将 FQCN 添加为 class 名称,因此 class: InicioBundle\Controller\Default
=> class: InicioBundle\Controller\DefaultController
当我们讨论主题时,您可以在您的操作中键入提示请求并将其用于 getSession() 或可能将 @session
服务注入您的控制器
我有这个控制器
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;
}
}
}
在 services.yml 中,我得到了这个:
parameters:
#parameter_name: value
services:
app.rolSession:
class: InicioBundle\Controller\DefaultController
arguments: ["i dont know how get paramets"]
问题是它不起作用,symfony return 一个错误 FileLoaderLoadException,services.yml 不包含有效的 YAML
在你的 services.yml
文件中 parameters:
之前有一个 space,也许删除它并且你的 yaml 应该是有效的。
此外,如果您不向构造函数传递任何参数,您只需删除 arguments: ["null"]
还有一点,IIRC 您需要将 FQCN 添加为 class 名称,因此 class: InicioBundle\Controller\Default
=> class: InicioBundle\Controller\DefaultController
当我们讨论主题时,您可以在您的操作中键入提示请求并将其用于 getSession() 或可能将 @session
服务注入您的控制器