Redirect with Codeigniter

Redirect with CodeIgniter

redirect()

URL Helper



The redirect statement in code igniter sends the user to the specified web page using a redirect header statement.

This statement resides in the URL helper which is loaded in the following way:

$this->load->helper('url');

The redirect function loads a local URI specified in the first parameter of the function call and built using the options specified in your config file.

The second parameter allows the developer to use different HTTP commands to perform the redirect "location" or "refresh".

According to the Code Igniter documentation: "Location is faster, but on Windows servers it can sometimes be a problem."

Example:

if ($user_logged_in === FALSE)
{
redirect('/account/login', 'refresh');
}

CodeIgniter 4 redirect function not working

as per CI 4

use

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

if you are using route then use

return redirect()->route('named_route');

How to redirect a page in CodeIgniter?

Use the redirect() function from the URL Helper.

EDIT:
load the url helper:

$this->load->helper('url');
if (condition == TRUE) {
redirect('new_page');
}

How to pass a data with redirect in codeigniter

So in the controller you can have in one function :

$in=1;
redirect(base_url()."home/index/".$in);

And in the target function you can access the $in value like this :

$in = $this->uri->segment(3);   
if(!is_numeric($in))
{
redirect();
}else{
if($in == 1){

}
}

I put segment(3) because on your example $in is after 2 dashes. But if you have for example this link structure : www.mydomain.com/subdomain/home/index/$in you'll have to use segment(4).

Hope that helps.

Not redirected to specific URL in Codeigniter 4

As per the Codeigniter forum, you can no longer use the redirect method in the constructor to redirect to any of the controllers.

Please refer the below link for more information

https://forum.codeigniter.com/thread-74537.html

It clearly states that redirect() will return a class instance instead of setting a header and you cannot return an instance of another class while instantiating a different class in PHP.

So that's why you can't use redirect method in constructor.

Instead, what I can suggest to you is that use the header method and redirect to your desired controller.

<?php namespace App\Controllers\Web\Auth;

class Register extends \App\Controllers\BaseController
{
function __construct()
{
if(session('username')){
header('Location: /dashboard');
}
}
}

If that's not feasible or difficult to achieve you can follow the below code

<?php namespace App\Controllers\Web\Auth;

class Register extends \App\Controllers\BaseController
{
function __construct()
{
//call to session exists method
$this->is_session_available();
}

private function is_session_available(){
if(session('username')){
return redirect()->to('/dashboard');
}else{
return redirect()->to('/login');
}
}
}

The 2nd solution will be more interactive than the first one. And make sure the method is private. So that it should not be called from other class instances.

The community team has also given a solution to look into the controller filter.

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

Please refer to the thread. I hope it may help you in finding a better solution.



Related Topics



Leave a reply



Submit