Welcome Guest, Not a member yet? Register   Sign In
How to use parameter in redirect function

As in CI4 redirect works like return redirect()->route(). I want to know How I can use a variable in redirect.

When I'm using simple route in redirect. It works.
Route: $routes->get('about-us', 'Main::about_us');

return redirect('about-us')
Its work fine.

But when I use parameter in redirect it shows an error. i.e (route cannot be found while reverse-routing.)

$studentid is some string.

return redirect('Student/ViewStudentProfile/'.$studentid');

I have also tried with route but not working.
return redirect()->route('Student/ViewStudentProfile/'.$studentid) ;

My route is 
$routes->get('Student/ViewStudentProfile/(:any)' , 'backend/Student_Controller::ViewStudentProfile');

I have spend a lot of time but not found any solution.
Any help would be greatly appreciated.

PHP Code:
$routes->get('Student/ViewStudentProfile/(:any)' 'backend/Student_Controller::ViewStudentProfile');

// should be like this see the last with the namespace. \ not /
$routes->get('Student/ViewStudentProfile/(:any)' 'backend\Student_Controller::ViewStudentProfile'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

Hi !

try this :

return redirect()->to(base_url('student/viewStudentProfile/'.$studentid));

(This post was last modified: 02-13-2021, 11:38 AM by ikesela.)

make sure to name the route :

$routes->get('ViewStudentProfile/(:segment)', 'Student_Controller::ViewStudentProfile/$1',['as' => 'profile']);

// LINK TO USE ( route_to is working for this)


// controller
public function ViewStudentProfile(int $studentId)
// ...your code here and return view(..)

or maybe can try:


(02-13-2021, 03:37 AM)neoneeco Wrote: Hi !

try this :

return redirect()->to(base_url('student/viewStudentProfile/'.$studentid));

Yes works fine. Thanks.

There is any way to use with only redirect().

Theme © iAndrew 2016 - Forum software by © MyBB