I have managed this by extending the Form request like this:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use App\Http\Controllers\Root\RootController;
use App\Http\Controllers\Root\ConfigRootController;
class Request extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
//
];
}
/**
* Intercept the request and make changes
*
*/
protected function getValidatorInstance()
{
$data = $this->all();
RootController::setConn($data['url']);
$Config = ConfigRootController::get_tl_config($data['url']);
if(isset($data['cookie'])){
$User = ConfigRootController::getCurrentUser($data['url'],$data['cookie']);
$GLOBALS['user']=$User;
}
$GLOBALS['CONFIG'] = json_decode($Config->getBody()->getContents());
return parent::getValidatorInstance();
}
}
However this solution has a limitation as it does not allow me to use the $request->validate()
method in the controller, which is a requirement.
Given that I am using laravel as a stateless API. I need to intercept all requests made and inject the script below right before the controller so that I can access and handle the config data as needed.
$data = $this->all();
RootController::setConn($data['url']);
$Config = ConfigRootController::get_tl_config($data['url']);
if(isset($data['cookie'])){
$User = ConfigRootController::getCurrentUser($data['url'],$data['cookie']);
$GLOBALS['user']=$User;
}
$GLOBALS['CONFIG'] = json_decode($Config->getBody()->getContents());
Here is an attempt via extending the Request class and applying the script in a constructor like so :
namespace App\Http\Requests;
// use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http;
use App\Http\Controllers\Root\RootController;
use App\Http\Controllers\Root\ConfigRootController;
class Request extends Http\Request
{
public function __construct()
{
$data = $this->all();
RootController::setConn($data['url']);
$Config = ConfigRootController::get_tl_config($data['url']);
if(isset($data['cookie'])){
$User = ConfigRootController::getCurrentUser($data['url'],$data['cookie']);
$GLOBALS['user']= json_decode($User->getBody()->getContents());
}
$GLOBALS['TL_CONFIG'] = json_decode($Config->getBody()->getContents());
}
}
This however results in a 500 internal server error from postman.