Backend Module with AJAX in Typo3 6.2.6? - typo3

I am working on an extension which contains both FE extension and BE Module. See below for my BE module
class Tx_XXXXXXX_Controller_PitslayersliderController extends Tx_Extbase_MVC_Controller_ActionController {
public function listAction(){
}
public function getdataAction($params = array(), \TYPO3\CMS\Core\Http\AjaxRequestHandler &$ajaxObj = NULL) {
//This is an AJAX Call Action and I am rendering all my contents through
//$this->view->assign();
//$this->view->render();
//Everything was working fine till 6.1 . But from 6.2 onwards this is not working.
}
}
ext_tables.php
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::registerAjaxHandler (
'xxxxxxxxxController::getdata',
'Tx_xxxxxxxxxx_Controller_xxxxxxxController->getdataAction'
);
Error from php_error_log
PHP Fatal error: Call to a member function assign() on a non-object in
Can any one help me with this issue ??

Related

Redirect if request authorization is failed in Laravel 5.5

I am trying to redirect request if authorization is failed for it. I have following code:
class ValidateRequest extends Request{
public function authorize(){
// some logic here...
return false;
}
public function rules(){ /* ... */}
public function failedAuthorization() {
return redirect('safepage');
}
}
By default I am redirected to the 403 error page, but I would like to specify some specific route. I noticed that method failedAuthorization() is run, but redirect() method does not work...
Previously this code worked well with Laravel 5.1 but I used forbiddenResponse() method to redirect wrong request. How can I fix it with new LTS version?
Looks like it is impossible to redirect() directly from the custom ValidateRequest class. The only solution that I found is create custom exception and than handle it in the Handler class. So, now it works with following code:
Update: The method redirectTo() was updated to make solution work on Laravel 6.x and higher
app/Requests/ValidateRequest.php
class ValidateRequest extends Request{
public function authorize(){
// some logic here...
return false;
}
public function rules(){
return [];
}
public function failedAuthorization() {
$exception = new NotAuthorizedException('This action is unauthorized.', 403);
throw $exception->redirectTo("safepage");
}
}
app/Exceptions/NotAuthorizedException.php
<?php
namespace App\Exceptions;
use Exception;
class NotAuthorizedException extends Exception
{
protected $route;
public function redirectTo($route) {
$this->route = $route;
abort(Redirect::to($route));
}
public function route() {
return $this->route;
}
}
and app/Exceptions/Handler.php
...
public function render($request, Exception $exception){
...
if($exception instanceof NotAuthorizedException){
return redirect($exception->route());
}
...
}
So, it works, but much more slower than I expected... Simple measuring shows that handling and redirecting take 2.1 s, but with Laravel 5.1 the same action (and the same code) takes only 0.3 s
Adding NotAuthorizedException::class to the $dontReport property does not help at all...
Update
It runs much more faster with php 7.2, it takes 0.7 s
If you are revisiting this thread because in 2021 you are looking to redirect after failed authorization here's what you can do:
You cannot redirect from the failedAuthorization() method because it is expected to throw an exception (check the method in the base FormRequest class that you extend), the side effect of changing the return type is the $request hitting the controller instead of being handled on FormRequest authorization level.
You do not need to create a custom exception class, neither meddle with the Laravel core files like editing the render() of app/Exceptions/Handler.php, which will pick up the exception you threw and by default render the bland 403 page.
All you need to do is throw new HttpResponseException()
In the Laravel reference API we can see its job is to "Create a new HTTP response exception instance." and that is exactly what we want, right?
So we need to pass this Exception a $response. We can pass a redirect or JSON response!
Redirecting:
protected function failedAuthorization()
{
throw new HttpResponseException(response()->redirectToRoute('postOverview')
->with(['error' => 'This action is not authorized!']));
}
So we are creating a new instance of the HttpResponseException and we use the response() helper, which has this super helpful redirectToRoute('routeName') method, which we can further chain with the well known with() method and pass an error message to display on the front-end.
JSON:
Inspired by this topic
throw new HttpResponseException(response()->json(['error' => 'Unauthorized action!'], 403));
Thats'it.
You don't have to make ANY checks for validation or authorization in your controller, everything is done in the background before it hits the controller. You can test this by putting a dd('reached controller'); at the top of your controller method and it shouln't get trigered.
This way you keep your controller thin and separate concerns :)
Sidenote:
forbiddenResponse() has been replaced after lara 5.4 with failedAuthorization()
You can do through a middleware/policy i think. I don't know if you can do it from the validation.
You can override the function from FormRequest like this below:
/**
* Handle a failed authorization attempt.
*
* #return void
*
* #throws \Illuminate\Auth\Access\AuthorizationException
*/
protected function failedAuthorization()
{
throw new AuthorizationException('This action is unauthorized.');
}
And redirect where you want.

cakephp: How to load Flash component in the plugin?

I am using CakePHP 3 and add a plugin cakphp-Notifier.
I want to add flash component in that plugin.
how to add cakephp default component in plugin?
Code:
NotificationManager.php
use Cake\Controller\Component\FlashComponent;
class {
// ...
private $Flash;
public function __construct( )
{
$this->Flash = new FlashComponent();
}
// ...
public function send {
$smsAPI->sendSms($numbers, $message, $sender);
$this->Flash->success(__('SMS sent .'));
// ...
}
I got this error:
Required parameter $registry missing.
Invocation parameters types are not compatible with declared.
First you must import the component on the top of your plugin file:
use Cake\Controller\Component\FlashComponent;
Then import the component inside your Class
var $components = array('Flash');
Then use your Flash functions
$this->Flash->set(__('Error!'));
Hope it works!

PHP-DI annotation not working

I have installed php-di 4.4 in a new custom project using composer.Im runing a xampp localhost with php 5.6.3 but I set netbeans for php 5.4 on this project. Im new to php-di, I did use annotations in my android projects but cant seem to make it work here. The code is simple, im testing out the injection to see how it works, here is the code:
// composer autoload
require_once __DIR__ . '/vendor/autoload.php';
// injection entry point
$builder = new DI\ContainerBuilder();
$container = $builder->build();
class ClassA
{
public function methodA()
{
echo 'methodA';
}
}
class ClassB
{
/**
* #Inject
* #var ClassA
*/
public $param;
public function methodB()
{
$this->param->methodA();
}
}
$b = new ClassB();
$b->methodB();
this is the error that i get:
Call to a member function methodA() on null in D:\Projects\profi\test.php on line 27
It's basic implementation I don't understand why it does not inject.
Thank you in advance.
PHP-DI cannot magically intercept when you create B (to inject A in B). You have to create B using PHP-DI:
$b = $container->get('ClassB');
$b->methodB();

Loading external library into ExpressionEngine plugin

I'm trying to load an external library into an ExpressionEngine plugin but am getting:
Message: Undefined property: Detector::$EE
In the plugin itself I've got:
public function __construct()
{
$this->EE->load->library('detector');
$this->EE =& get_instance();
}
and my folders are set up like:
detector
-libraries
--Detector.php
-pi.detector.php
What am I doing wrong?
Having moved past the loading library error, I'm now getting an 'undefined variable' error with the following code:
public function detector()
{
return $ua->ua;
}
public function user_agent()
{
return $ua->ua;
}
That's if I have {exp:detector:user_agent} in my template. If I {exp:detector} I get no output.
you should change your code like this:
$this->EE =& get_instance();
$this->EE->load->add_package_path(PATH_THIRD.'/detector');
$this->EE->load->library('detector');
First initialize the $this->EE variable, then you can load the library. So in this case it would be
$this->EE->detector->user_agent();

Probable reasons why autoloading wont work in Zend Framework 1.10.2?

Iam writing an application using Zend Framework 1.10.2.
I created few model classes and a controller to process them.
When Iam executing my application and accessing the admin controller. Iam seeing this error.
Fatal error: Class 'Application_Model_DbTable_Users' not found in C:\xampp\htdocs\bidpopo\application\controllers\AdminController.php on line 16
The error clearly shows its an autoloading error.
Hence I wrote this code in the bootstrap file.
protected function initAutoload()
{
$modeLoader = new Zend_Application_Module_AutoLoader(array
('namespace'=>'','basePath'=>APPLICATION_PATH ));
//echo(APPLICATION_PATH);
return $modeLoader;
}
Still the error remains :( . Can anyone suggest me what Iam missing out here?
This is the location of the Model Users class.
C:\xampp\htdocs\bidpopo\application\models\DbTable\Users.php
This is its code.
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
//put your code here
protected $_name='users';
public function getUser($id)
{
$id = (int)$id;
$row = $this->fetchrow('id='.$id);
if(!$row)
{throw new Exception("Could not find row id - $id");}
return $row->toArray();
}
public function addUser($userDetailArray)
{
$this->insert($userDetailsArray);
}
public function updateUser($id,$userDetailArray)
{
$this->update($userDetailArray,'id='.(int)$id);
}
public function deleteUser($id)
{
$this->delete('id='. (int)$id);
}
}
This is the Admin Controller's code
class AdminController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->view->title= "All Users";
$this->view->headTitle($this->view->title);
$users = new Application_Model_DbTable_Users();
$this->view->users = $users->fetchAll();
}
public function addUserAction()
{
// action body
}
public function editUserAction()
{
// action body
}
public function deleteUserAction()
{
// action body
}
You application classes don't follow the proper naming convention for the namespace you've set. The Zend_Application_Module_AutoLoader is a little different than the normal autoloader in that it doesn't simply change the '_' in the class name with '/'. It looks at the second part of the class name and then checks a folder for the existence of the class based on that.
You need to change the line:
$modeLoader = new Zend_Application_Module_AutoLoader(array(
'namespace'=>'Application',
'basePath'=>APPLICATION_PATH
));
This means it will autoload all module classes prefixed with 'Application_'. When it the second part of the class is 'Model_' it will look in "{$basePath}/models" for the class. The '_' in the rest of the class name will be replaced with '/'. So the file path of the file will be "{$basePath}/models/DbTable/Users.php".
Read more here.