problem with custom controller constructor |
Hi,
in the constructor i loaded models, settings and checked if access for the user is possible. All things i need throughout the controller. This is a good place until i need access to the request object. Why is this bad practice? What is the better way except overwriting the initController function or loading the request service ?
Bad practice may have been an exaggeration.
Of course you can use __construct() unless you don't use the request object or something that initController() initializes. True bad practice is try to use uninitialized things. You don't need to do that. If you want to do something with the request object, you can use Controller Filters.
I solved this by instantiating the following classes which are already instantiated in BaseController, parent class:
use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface;
(05-29-2021, 10:18 AM)tino Wrote: i have a SignupController with some methods in it. Hi, Try to create app/Filters create an auth file there to validate exp: auth.php <?php namespace App\Filters; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use CodeIgniter\Filters\FilterInterface; class Auth implements FilterInterface { public function before(RequestInterface $request, $arguments = null) { // if user not logged in if(! session()->get('logged_in')){ // then redirct to login page return redirect()->to('/login'); } } //-------------------------------------------------------------------- public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) { // Do something here } } After that, open the “Filters.php” file located in the “app/Config” folder, then find the following code: public $aliases add one line with public $aliases = [ 'csrf' => \CodeIgniter\Filters\CSRF::class, 'toolbar' => \CodeIgniter\Filters\DebugToolbar::class, 'honeypot' => \CodeIgniter\Filters\Honeypot::class, 'auth' => \App\Filters\Auth::class,---------------------------------this line Next, open the “Routes.php” file located in the “app/Config” folder, then find the following code: $routes->get('/dashboard', 'Dashboard::index',['filter' => 'auth']); this is a better approach to deal with redirect to login
I have tried to move the stuff I usually do in __constructor to initController() method. It seems to work (and I can avoid initializing the things again that have been initialized in the BaseController's initController() function. However, the vscode shows a nasty warning:
Quote:Method 'App\Controllers\Booking::initController()' is not compatible with method 'App\Controllers\BaseController::initController()'.intelephense(1038)What should I make of that? Is intelephense wrong, or am I not seeing something? Hard to ignore it psychologically, it triggers me here is my modified controller (with the previous code commented out: PHP Code: class Booking extends BaseController
==
Donatas G. |
Welcome Guest, Not a member yet? Register Sign In |