Welcome Guest, Not a member yet? Register   Sign In
Redirect Help
#1

CodeIgniter4 / Ubuntu Linux 20.04.01 LTS / PHP 7.4.3

return redirect ... <= doesn't seem to do anything using any of the following attempts, when invoked from any function, any Model:

NOTE: 'namedroute' is defined in app/Config/Routes.php, and works fine when I use it directly, i.e.
https://www.domain.com/namedroute gets me there


Code:
return redirect('namedroute')
return redirect()->route('namedroute');
return redirect()->to('/namedroute');
return redirect()->to('https://www.domain.com/namedroute');
return redirect()->to(base_url('namedroute'));
return redirect()->to(site_url('namedroute'));
return redirect()->back();

In all of these cases, I just get a blank page.

These attempts return CodeIgniter error "route cannot be found while reverse-routing" (because it's not a valid route):

Code:
return redirect('/namedroute');
return redirect()->route('/namedroute');

There are no errors logged (unless CI4 throws an error). Just ... nothing happens ... a blank page and no change in the URI.
TIA for any advice.
Reply
#2

Please note that I can use redirect()->route('namedroute') only in a Controller function, but it does not work anywhere else.

From the CI4 manual:
Quote:Global Functions and Constants
CodeIgniter provides a few functions and variables that are globally defined, and are available to you at any point. These do not require loading any additional libraries or helpers.


redirect() is included in this group of "global functions".

Still hoping for any tips to be able to use redirect() in a Model. Thank you.
Reply
#3

Because it may be useful, in addition to the redirect() issue(s), the cookie helper seems to be borked.

Doesn't matter if I put this in a Controller or a Model, it does nothing that I can find, and does not throw any errors.


Code:
helper('cookie');
set_cookie('testcookie','testdata');
$c = get_cookie('testcookie');
print_r($c);

And there is no cookie set in my browser.

Could this be an installation problem?
Most everything that I've used in this installation works as expected, but not redirect() or the cookie helper.

Thanks, again.
Reply
#4

Yes, it sounds like an installation problem or setup of Ubuntu Linux.

Which I can not help you on because I use Windows 10 Pro x64
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Redirect.
The model works with data.
Redirecting is the responsibility of the controller.
What you want to do is violate the MVC pattern.

Cookie.
1. To set an active cookie, you need to pass expires as an argument to the set_cookie function.
If you don't pass the expires argument then the time from the past will be set (equivalent to deleting)
2. set_cookie sets a cookie in the Response class.
get_cookie gets data from the IncomingRequest class.
You will not receive cookies until your browser sends them.
Reply
#6

The cookie helper is not borked.
Reply
#7

Thank you all for your comments and tips. Here are my takeaways:

1) redirect() is not actually a "global" function that is available "at any point", rather, its use is restricted to Controllers.

2) Cookies MUST set an expiration time, and can NOT be read as soon as they are created, because CI4 needs to handle them using different processes. This is different from traditional cookie management which does not require an expiry time (if you want them to last as long as the browser is open, then be removed when the browser closes) and which can be read as soon as they are created, seeing as how they're traditionally a browser construct that does not rely on any application-level classes.

Good information which is not communicated by the docs. I appreciate you all taking the time to comment.
Reply
#8

Hmm ... well, still no luck with cookies.

In a Model (are cookies also restricted to Controllers, like redirect() is?):


Code:
helper('cookie');
set_cookie('mudcookie','test value',3600);

Then in any View or any other Model:


Code:
helper('cookie');
$cookie = get_cookie('mudcookie');
print_r($cookie);

prints nothing.

There is a page load in between setting and getting, but still nothing. No cookie in the browser 'Application' either.

If I use the php $_COOKIE I can set it, but can't read it in CI4 (error: "Function name must be a string"):

Code:
print_r($_COOKIE('mudcookie'));

and setting it with $_COOKIE obviously doesn't create a cookie that CI4 can read:


Code:
helper('cookie');
print_r(get_cookie('mudcookie'));

Anyway. Just updating for future readers. I'm going to use sessions, which seem to be functioning normally.

If anyone sees a problem with the test code, or with where I'm putting it or how I'm testing it, I'd appreciate a response. TIA.
Reply
#9

The model works fine with the cookie.
Reply
#10

(This post was last modified: 02-08-2021, 05:37 PM by stupidscript.)

Thank you for your assistance.

Apologies for getting off-topic with this thread.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB