• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use parameter in redirect function

#1
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.
Like
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.
Reply

#2
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 )
Reply

#3
Hi !

try this :

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

#4
make sure to name the route :
EXAMPLE:

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

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

route_to('profile',$student_id);

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

or maybe can try:

redirect()->route('route_name',$param)
Reply

#5
(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().
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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