How can definition a global variable for use in all function controller
class TestController extends Controller
{
private $x;
public function index()
{
$this->$x ='22';
}
public function send_message()
{
echo $this->$x;
}
}
Write $this->x rather than $this->$x
class TestController extends Controller
{
private $x;
public function index()
{
$this->x ='22';
}
public function send_message()
{
echo $this->x;
}
}
If you want to make global variable in controller, the following code will work definitely:
private $x = 22;
public function index()
{
print_r($this->x);
die();
}
Related
I have a problem with binding params. My code is:
interface MessageInterface
{
public function getMessage() : string;
}
class MessageType1 implements MessageInterface
{
public function getMessage(): string
{
return 'Type 1';
}
}
class MessageType2 implements MessageInterface
{
public function getMessage(): string
{
return 'Type 2';
}
}
class TestController extends AbstractController
{
private $m;
public function __construct(MessageInterface $m)
{
$this->m = $m;
}
/**
* #Route("/message", name="message")
*/
public function index(MessageInterface $m): Response
{
return $this->render('test/index.html.twig', [
'message' => $m->getMessage(),
]);
}
}
How to bind in service.yaml TestController constructor param to MessageType1 class and method index param to MessageType2 class ?
I have a 2 Bundles.
Bundle 1:
class EntityOne {
private $_foo;
public function setFoo($foo)
{
$this->_foo = $foo;
return $this;
}
public function getFoo()
{
return $this->_foo;
}
}
My Service (service.yml)
service:
MyFooProject\Entity:
class: MyFooProject\Entity\EntityOne
Bundle 2:
class EntityTwo extends \MyFooProject\Entity\EntityOne {
private $_bar;
public function setBar($bar)
{
$this->_bar = $bar;
return $this;
}
public function getBar()
{
return $this->_bar;
}
}
My Service (service.yml)
service:
MyBarProject\Entity\EntityTwo
parent: MyFooProject\Entity\EntityOne
Now I want call Method setBar() in Bundle 1 but Symfony didn't find Method setBar() from Bundle 2
function test()
{
$oEntity = $this->get(MyFooProject\Entity\EntityOne::class);
$oEntity->setFoo("foo");
$oEntity->setBar("bar"),
}
How can I solve the problem?
Please i need some help:
I have the following services:
SERVICE CONFIGURATION IN services.yml
services:
xpad.producto_repository:
class: Xpad\ProductoBundle\Entity\ProductRepository
factory_service: doctrine.orm.clientes_entity_manager
factory_method: getRepository
arguments:
- Xpad\ProductoBundle\Entity\Product
backend_cliente.producto_filters:
class: Xpad\BackendClienteBundle\Filters\ProductFilters
calls:
- [setRepository, ["#xpad.producto_repository="]]
scope: container
AND THE CLASS FOR backend_cliente.producto_filters IS:
namespace Xpad\BackendClienteBundle\Filters;
use Xpad\ProductoBundle\Entity\ProductRepository;
class ProductFilters
{
private $_queryBuilder;
public function getQueryBuilder()
{
return $this->_queryBuilder;
}
public function setQueryBuilder($queryBuilder)
{
$this->_queryBuilder = $queryBuilder;
}
public function setRepository(ProductRepository $productRepository = null)
{
if($this->_queryBuilder == null)
{
$this->_queryBuilder = $productRepository->createQueryBuilder('p');
}
}
}
AND I HAVE THE FOLLOWING ACTION IN ONE OF MY CONTROLLERS:
class ProductController extends Controller
{
............
public function indexAction(Request $request)
{
//SOME CODE
$service_filter = $this->container->get('backend_cliente.producto_filters');
$queryBuilder = $service_filter->getQueryBuilder();
//SOME OTHER CODE
}
MY PROBLEM IS: ANYTIME THAT THE indexAction is execute I GOT A NEW INSTANCE OF backend_cliente.producto_filters SERVICE AND I DON'T KNOW WHY. I NEED AND UNIQUE INSTANCE AS A SINGLETON BECAUSE A HAVE THE $_queryBuilder ATRIBUTTE AND I NEED TO GET THE VALUE OF IT JUST MODIFY ITS VALUE WHEN IS NEEDED;
PLEASE HELP I DON KNOW WHAT I'M DOING WRONG.
Do you have to use the class as a service?
Why don't you use a Singleton, without using a service? Like this:
namespace Xpad\BackendClienteBundle\Filters;
use Xpad\ProductoBundle\Entity\ProductRepository;
class ProductFilters {
private $_queryBuilder;
private static $reference = null;
public function getInstance(){
if (self::$reference === null)
self::$reference = new ProductFilters();
return self::$reference;
}
private function __construct(){}
public function getQueryBuilder()
{
return $this->_queryBuilder;
}
public function setQueryBuilder($queryBuilder)
{
$this->_queryBuilder = $queryBuilder;
}
public function setRepository(ProductRepository $productRepository = null)
{
if($this->_queryBuilder == null)
{
$this->_queryBuilder = $productRepository->createQueryBuilder('p');
}
}
}
And call it with:
...
$filter = ProductFilters::getInstance();
...
How to call a Class in a Function (Flex 4)?
Example:
public class A {
private function a();
private function b();
....
}
This is my question-->
private function loadMain() {
**call class A in here**
}
Who have any ideas ???
Thank for watching
Is this what you mean?
Class A:
public class A {
public function a();
public function b();
}
Function loadMain:
private function loadMain() {
var instance:A = new A();
instance.a();
instance.b();
}
I have implemented a new twig extension and I have some text which had to be translated.
Unfortunately when I use a code label it appears as a sample text.
I mean when twig render this following extension, it displays: 5 entity.years instead of 5 years for example:
class MyExtension extends \Twig_Extension {
public function getFilters()
{
return array(
'myextension' => new \Twig_Filter_Method($this, 'myextension'),
);
}
public function myextension ($myId)
{
// ....
// Some operations concerning $myId...
// ....
if($myId!=0) {
$res = $myId. ' '.'entity.year';
} else {
$res = ($months == 0 ? $days.'entity.days' : $months.'entity.months');
}
return $res;
}
}
Where entity.years, entity.months, entity.days is defined into my translations folder.
Inject the translator service into your extension and use it. For example:
class MyExtension extends \Twig_Extension
{
private $translator;
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
// ...
public function myMethod()
{
return $this->translator->trans('my_string');
}
}