CodeIgniter Forums
redirect a user from a main controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: redirect a user from a main controller (/showthread.php?tid=79641)



redirect a user from a main controller - Z4K7 - 07-13-2021

I am created a login project
I am presenting a difficulty since at the time of the user entering the login, it is not redirecting to the web that I want but it remains in the login validator
the file structures is yes

PHP Code:
<?php 

namespace App\Controllers;


class 
Admin_Controller extends BaseController
{
 


 public function 
ingresar_panal()
 {

 return 
redirect()->to('panel');

 }

}
?>


this is my main controller where I would give the whole body of the more controllers

this is the login controller

PHP Code:
<?php

namespace App\Controllers\admin;
use 
App\Controllers\Admin_Controller;

class 
Login extends Admin_Controller


 public function 
index()
 {

 echo 
view('admin/login');
 
 }
 public function 
validacion()
 {
 
$this->ingresar_panal();
}
The login is validated without any problembut when you click on the login buttonit is not redirecting where you would have to enter if not only it remains in login


ya valide desde la configuraciĆ³n de Router 
PHP Code:
$routes->get('/''Login::index');
$routes->Post('login''Login::validacion');
$routes->get('panel''Panel::index'); 

It could help me what is the failure since if I place the redirection from the following way if it works


PHP Code:
<?php

namespace App\Controllers\admin;
use 
App\Controllers\Admin_Controller;

class 
Login extends Admin_Controller
{

public function 
index()
{

echo 
view('admin/login');

}
public function 
validacion()
{
return 
redirect()->to('panel');





and it is addressed where I want to but I don't understand if I do it from the main controller and call that one, it works, it is not working, it could help me or clarify what is happening

Thank you


RE: redirect a user from a main controller - paliz - 07-14-2021

Use authentication filter to check whether use log in or not

https://codeigniter4.github.io/userguide/incoming/filters.html

Before function check user status

You can not repeat code for every
Ctl once for all


RE: redirect a user from a main controller - superior - 07-14-2021

I've got to agree with @paliz on this one, the Filter is the best choice to accomplish this. You can always exclude certain pages to bypass the authentication part.