• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with redirect and routes

#1
Please,
someone can explain me (as i'm six years old) how to work routing on CI4? I have read entaire documentation and every post in this forum but apparently nothing work. Probably i have lost some parts or i don't know.

I have 2 pages.

in second pages if is present a specific condition i would to redirect the user to first page, so:

redirect()->route('landing');


In this moment i put the redirect in __constructor of Controller, to test the redirect. Nothing, redirect is completly ignored, controller continue to live as redirect not exists.Oviusly i have tested all variants of redirect( ES: redirect()->route('/landing')redirect()->to('landing');).
Only in the first variant i recive this error: route cannot be found while reverse-routing .

In my Routes.php i have added:

$routes->add('a, 'ControllerA::index');
$routes->add('b', 'ControllerB::index');


Oviusly if i use the classic "header:location......." all work but i think that's is important use the CI4 routing for other particular situation

I hope that someone can help me, thanks
Reply

#2
Try adding return. Such as return redirect()->route('landing');

But do not put it in the construct. Constructors should not return anything. Instead put that in another method, like index()
Reply

#3
hi,
thanks for help.
Unfortunatly not work. I tried:

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

The current controller is rendered normally without redirect. I have tested another way.

echo "test";
return redirect()->route('landing');
exit;

This test show "test" in page but exit as redirect, is ignored.If i remove return (in this last test) exit work but redirect not work so i show a blank page with print "test".

Thanks for any other help you can give me.
Reply

#4
First you will need to make sure make sure automatic routing is enabled and/or you have defined your custom routes in app\Config\Routes.php.  You can find the documentation here: https://codeigniter.com/user_guide/incom...uting.html.

To redirect to a specific route try the following from your controller:

PHP Code:
return redirect()->to('my_uri'); 

"my_uri" should be a route that is defined in your Routes.php file or correspond to an automatic route (e.g. 'users/profile', 'dashboard', 'blog/post/1234', etc.)
Reply

#5
For redirects in the constructor you should be using filters.

You can try this not sure if it will work. not tested.

PHP Code:
// The redirect()->to('this') uses the as in the route below.
$routes->add('home/something''Home::something', ['as' => 'landing']);

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

Change the home/something to your controller/method
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#6
In the problem reported by @madsex we have the following:

There are two views. In the second view from the appearance of a certain condition the user must be redirected to the first view:

redirect()->route('landing');
Well, there is the root of the whole problem. If a certain "condition" needs to exist in order for the user to be redirected to the first view, it is necessary to check whether the condition was created or not, for example:
PHP Code:
if ($condition != null) {
    redirect()->route('landing');

or something like that.

I would like to talk more about the use of routers, which in my view are used indiscriminately, without an assessment as to whether or not it is the best option.

In the example above, the following codes are added to routes.php:
PHP Code:
routes-> add ('a,' 'ControllerA::index ');
routes-> add ('b''ControllerB::index'); 

Assuming that the condition has been satisfied, you make the system search for the reference informed in Routes.php, after having found the references, checking if the encoding is correct, it will go to the indicated controller and then display the page to the user.

If it had been used instead:
PHP Code:
if ($condition != null) {
      return redirect()->to('/ControllerA/index');
exit; 

The time spent, theoretically, would be 50% less and a single line of code would suffice.

I am very tired today, I woke up at 5:00 am and it is now 12:49 am.

Perhaps later I will continue with my comment.

I hope that the solution pointed out is right.
Reply

#7
Thanks for help, i tried your solutions but nothing change.

Maybe we need a recap about my situation.

Automatic routing work, if i try to navigate into xxxx.it/landing or /login or /registration all works fine.

I my controller registration i have a situation similar as to described by wdeda:

Code:
if ($control == '0') {
//header("location: ".base_url()."");
return redirect()->to('landing');
exit;
}

Has you can see actually i'm using header to redirect and it's work fine but i have commented that to try your solutions.
Landing i a test page/controller that i have create.

Routes.php contain this strings that i have insert:
Code:
$routes->add('/registration', 'Registration::index');
$routes->add('/landing', 'Landing::index');

I tried all yours solutions but nothing change, instead of a redirect i obtain only the refresh of the page where i'm.

Thanks for help
Reply

#8
Take the forward slash of the front of your routes.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.