CodeIgniter Forums
what happen is error with redirect to codeigniter 4.3.3 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: what happen is error with redirect to codeigniter 4.3.3 (/showthread.php?tid=87286)



what happen is error with redirect to codeigniter 4.3.3 - startup - 04-04-2023

i have function below, but redirect() ->to('auth/home'); dont work, can someone show me what is error?
Code:
404
Sorry! Cannot seem to find the page you were looking for.


PHP Code:
public function check(){

        $validation $this->validate([
            'email' => [
                'rules'  => 'required|valid_email|is_not_unique[users.email]',
                'errors' => [
                    'required' => 'Email is required.',
                    'valid_email' => 'Please check the Email field. It does not appear to be valid.',
                    'is_not_unique' => 'Email not registered in our server.',
                ],
            ],
            'password' => [
                'rules'  => 'required|min_length[5]|max_length[20]',
                'errors' => [
                    'required' => 'Password is required.',
                    'min_length' => 'Password must have atleast 5 characters in length.',
                    'max_length' => 'Password must not have characters more thant 20 in length.',
                ],
            ],
        ]);

        if(!$validation){
            return  redirect()->to('auth/login')->with('validation'$this->validator)->withInput();
        }else{
            $email $this->request->getPost('email');
            $password $this->request->getPost('password');
            $userModel = new UserModel();
            $user_info $userModel->where('email'$email)->first();

            $check_password Hash::check($password$user_info['password']);
            if( !$check_password ){
                return  redirect()->to('auth/login')->with('fail''Incorect password.')->withInput();
            }else{
                $session_data = ['user' => $user_info];
                session()->set('LoggedUser'$session_data);
                $data['xin_chao']='abc';
              
                return  redirect
()->to('auth/home');

            }
        }
    

thank you for reading


RE: what happen is error with redirect to codeigniter 4.3.3 - Muzikant - 04-19-2023

Hi. Check if you have correctly set destination route auth/home in app/Config/Routes.php. It could be something like this:

PHP Code:
$routes->get('auth/home''Auth::home'); 

You could also check if you have correct variables $baseURL and $indexPage in app/Config/App (or in .env file if you are using it), because redirect()->to() is based on these values.



If it does not work, then try only redirection on a fresh CodeIgniter's installation.

app/Config/App.php
PHP Code:
// ...
// change these variables
    public string $baseURL 'http://localhost:8080/'// change if you are not using "php spark serve" or if it started on a different port
    public string $indexPage ''// removing index.php proved to me in all cases
// ... 

app/Config/Routes.php
PHP Code:
// ...
// add these routes
$routes->get('from''Home::from');
$routes->get('to''Home::to');
//... 

app/Controller/Home.php
PHP Code:
// ...
// add these methods
    public function from()
    {
        return redirect()->to('to');
    }
    public function to()
    {
        echo 'Successfully redirected to home/to.';
    }
// ... 

Access this URL in a web browser (based on $baseURL): http://localhost:8080/from and watch if it is working. If it will work, then the problem is somewhere in your code or settings.


RE: what happen is error with redirect to codeigniter 4.3.3 - SubrataJ - 04-19-2023

if you are using validation,  it is pretty much sure you want to g back to the same page with the validation error in this case instead of using 
PHP Code:
return redirect()->to() 
  you can use
PHP Code:
redirect()->back() 
, although the way you have done it should work, just check your routes in the console 
Code:
php spark routes
.
for more info check redirect-and-validation-errors


RE: what happen is error with redirect to codeigniter 4.3.3 - byoosdigital - 04-22-2023

thjs code under line  working  good
header("Location:".base_url('login'));


RE: what happen is error with redirect to codeigniter 4.3.3 - SubrataJ - 04-22-2023

(04-22-2023, 02:29 AM)byoosdigital Wrote:
thjs code under line  working  good
header("Location:".base_url('login'));

your code won't work until you use die or exit after header function.


RE: what happen is error with redirect to codeigniter 4.3.3 - byoosdigital - 04-24-2023

(04-22-2023, 02:29 AM)byoosdigital Wrote:
thjs code under line  working  good
header("Location:".base_url('login'));

It's  OK
//return redirect()->to('login');
header("Location:".base_url('login'));//BYOOS_tag
exit();