I wrote a Command Controller that handles data import from an URL.
pseudo-syntax is like this:
class ImportCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandController
{
public function importCommand($auth){
$data = file_get_content();
}
}
this works.
But when I try to call that command from the Action Controller of my backend Module I get errors.
Heres the code:
ActionController:
class ImportController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
/**
* #var \Vendor\MyExt\Command\ImportCommandController importCommandCtrl
* #inject
*/
protected $importCommandCtrl;
public function importAction()//($url,$usr,$pass)
{
//$this->importCommandCtrl = GeneralUtility::makeInstance('Vendor\MyExt\Command\ImportCommandController');
$this->importCommandCtrl->testCommand();
}
}
When I call importAction() like this, I get:
"Call to a member function testCommand() on null"
When I uncomment the makeInstance, I get:
"Call to a member function get() on null"
Sadly, this topic is documente rather poorly in the TYPO3 Docs.
Can someone help me on this or point me to the right direction?
I'd like to slightly alter the answer already given by René and add some code examples. I also recommend to put your import logic into a dedicated class, e.g. ImportService:
namespace Vendor\MyExt\Service;
use TYPO3\CMS\Core\SingletonInterface;
class ImportService implements SingletonInterface
{
public function importData()
{
// import logic goes here
}
}
You can now inject this class as a dependency of your CommandController and your ActionController:
class ImportController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
/**
* #var \Vendor\MyExt\Service\ImportService
* #inject
*/
protected $importService;
public function importAction()
{
$this->importService->importData();
}
}
class ImportCommandController extends \TYPO3\CMS\Extbase\Mvc\Controller\CommandControlle
{
/**
* #var \Vendor\MyExt\Service\ImportService
* #inject
*/
protected $importService;
public function importCommand()
{
$this->importService->importData();
}
}
The use of an CommandController in an ActionController is not recommended because they have different envoiroment variables.
If you want to use some code on more position it's recommanded to use Utility classes.
So create an Class in the my_ext/Classes/Utility/ directory call the class something like ImportUtility and try to code your import independed from some controller.
Related
I'd like to override the following method in Laravel's Illuminate\Foundation\Vite class:
/**
* Generate a script tag for the given URL.
*
* #param string $url
* #return string
*/
protected function makeScriptTag($url)
{
return sprintf('<script type="module" src="%s"></script>', $url);
}
...by adding a "defer" attribute to the script tag. How would I go about doing this, as this is a protected function?
May be like that :
<?php
namespace myApp;
use Illuminate\Foundation\Vite as IllVite;
class myClass extends IllVite{
//...
protected function makeScriptTag($url){
return sprintf('<script type="module" src="%s" defer></script>', $url);
}
//...
}
In the controller(s) which call "Vite", change :
use Illuminate\Foundation\Vite;
by
use myApp\myClass;
The svgta is right, if you use that method in another place in your app.
But, if you want to change only that particular behavior, and not to use it in other places, then you can rewrite the original class and replace it by binding changed class to the app in app/Providers/AppServiceProvider:
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind(
'Illuminate\Foundation\Vite', // original class that will be replaced
'App\VendorRewrites\ViteChanged' // custom class that will be injected
);
}
............
Another post that can help: Laravel 6-7 How Can I Override/Change a Vendor Class?
I am trying to document my code and to remove my IDE warnings on my code. I'm working with IntelliJ IDEA.
I have two classes, the first is an abstract Controller like this :
abstract class Controller {
/** #var Service $service */
public static $service;
...
}
The second one is a UserController, which extends the first :
/** #property UserService $service */
abstract class UserController extends Controller {
public static function testService() {
static::$service->testMethod();
}
...
}
My issues :
If I declare a new function on the UserService without the #property tag, my IDE is telling me that Method 'testMethod' not found in Service
If I use the #property, the IDE is telling me that Field '$service' not found in UserController
How can I fix this ?
I would solve the need via a property override... this shouldn't hinder the code logic itself:
abstract class Controller {
/** #var Service */
public static $service;
...
}
abstract class UserController extends Controller {
/** #var UserService */
public static $service;
public static function testService() {
static::$service->testMethod();
}
...
}
Side note: the #var syntax for class properties doesn't need the variable name repeated... that's only needed when #var is used to denote type on a local variable. In the class property case, including the property name is actually just using the property name as the first word of the "description" portion.
I'm using phalcon in a new project but i realized that phpStorm is not understanding my own injectors, for example:
/**
* Registering the helper component
*/
$di->set('helper', function () {
$helper = new \Frontend\Helpers\Common();
return $helper;
});
from my controller i can do that:
$this->helper->getHelp();
and it is working fine, but the IDE do not autocomplete my code. Of course i could do that:
/** #var \Frontend\Helpers\Common $helper */
$helper->...
In this case i can see all methods, but then each time when i need to use the helper i have to put this comments.
I would like to know if there is a way to use owns injectors and make the IDE abble to understand the code?
You can use this workaround.
services.php
/**
* Registering the helper component
*/
$di->set('helper', function () {
$helper = new \Frontend\Helpers\Common();
return $helper;
});
AbstractController
/**
* My Abstract controller
*
* #property \Frontend\Helpers\Common helper
*/.
class AbstractController extends \Phalcon\Mvc\Controller
{
}
TestController
class TestController extends AbstractController
{
public function indexAction()
{
$this->helper->...
}
}
I Want to extend Symfony2 Controller to my project that is using API but I am having error of a non object use getParameter() function look at my code:
namespace Moda\CategoryBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class ApiController extends Controller
{
/**
* #var String
*/
protected $_host;
/**
* #var String
*/
protected $_user;
/**
* #var String
*/
protected $_password;
public function __construct()
{
$this->_host = $this->container->getParameter('api_host');
$this->_user = $this->container->getParameter('api_user');
$this->_password = $this->container->getParameter('api_password');
}
}
And next Controller
namespace Moda\CategoryBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class CategoryController extends ApiController
{
/**
* #Route("/category", name="_category")
* #Template()
*/
public function indexAction()
{
return array('name' => 'test');
}
}
And the end, I got this Fatal Error:
FatalErrorException: Error: Call to a member function getParameter()
on a non-object in (..)
I try to use $this->setContainer() but it doesn't work. Do you have any idea how can I slove this problem?
If your controller is not defined as service, The constructor execution of the controller is not persisted.
You have two options to solve your situation:
Define the controller as a service and inject the parameters you need using dependency injection.
Add an init method in the controller, or on a parent abstract controller, and call the init method, before the action you need to have these parameters available;
You cant use container in Controller __construct at reason that when constructor called where is none container set yeat.
You can simply define some simple methods in controller like
class ApiController extends Controller
{
protected function getApiHost()
{
return $this->container->getParameter('api_host');
}
}
I wonder if something crazy like this would work? Instead of overriding the constructor, override the setContainer method? I haven't tried it...just thinking out loud.
namespace Moda\CategoryBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ApiController extends Controller
{
/**
* #var String
*/
protected $_host;
/**
* #var String
*/
protected $_user;
/**
* #var String
*/
protected $_password;
public function setContainer(ContainerInterface $container = null)
{
parent::setContainer($container);
$this->_host = $this->container->getParameter('api_host');
$this->_user = $this->container->getParameter('api_user');
$this->_password = $this->container->getParameter('api_password');
}
}
I am currently looking a this piece of code from a module called ZfcUser for Zend 2:
namespace ZfcUser\Controller;
use Zend\Form\Form;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Stdlib\Parameters;
use Zend\View\Model\ViewModel;
use ZfcUser\Service\User as UserService;
use ZfcUser\Options\UserControllerOptionsInterface;
class UserController extends AbstractActionController
{
/**
* #var UserService
*/
protected $userService;
.
.
public function indexAction()
{
if (!$this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute('zfcuser/login');
}
return new ViewModel();
}
.
.
}
In the namespace ZfcUser\Controller\Plugin:
namespace ZfcUser\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use ZfcUser\Authentication\Adapter\AdapterChain as AuthAdapter;
class ZfcUserAuthentication extends AbstractPlugin implements ServiceManagerAwareInterface
{
/**
* #var AuthAdapter
*/
protected $authAdapter;
.
.
/**
* Proxy convenience method
*
* #return mixed
*/
public function hasIdentity()
{
return $this->getAuthService()->hasIdentity();
}
/**
* Get authService.
*
* #return AuthenticationService
*/
public function getAuthService()
{
if (null === $this->authService) {
$this->authService = $this->getServiceManager()->get('zfcuser_auth_service');
}
return $this->authService;
}
My Questions:
From indexAction(), the controller plugin is called without being instantiated ($this->zfcUserAuthentication()->hasIdentity()), do controller plugins always work like this?.
What really happens in the hasIdentity()? I see getAuthService() returning something but not hasIdentity().I am not familiar with this type of advanced class implementation of function calling so I would truly appreciate any explanation here or topic I should look into.
I can't answer your first question, but regarding your second question:
The getAuthService() method in your code returns an AuthenticationService object, which has a hasIdentity() method.
So there are two different hasIdentity() methods:
In the AuthenticationService class (source code here).
In the ZfcUserAuthentication class which you're looking at.
This line of code in the ZfcUserAuthentication class:
return $this->getAuthService()->hasIdentity();
does three things:
$this->getAuthService() returns an AuthenticationService object.
The hasIdentity() method of that AuthenticationService object is then called, and it returns a boolean.
That boolean is then returned.
Imagine splitting the code into two parts:
// Get AuthenticationService object Call a method of that object
$this->getAuthService() ->hasIdentity();
Hope that helps!
All sorts of plugins in Zend Framework are managed by plugin managers, which are subclasses of AbstractPluginManager which is subclasss of ServiceManager.
$this->zfcUserAuthentication() proxies by AbstractController to pluginmanager internally.
AuthenticationService::hasIdentity() checks if something was added to storage during successful authentication attempt in this or previous request:
See here