0

I have a controller that calls my model to insert a new record, returned the id of the new record, and then does a redirect to display the new record. But I get an error: "The route for '/r/xx' cannot be found". My Routes.php has a route:

 $routes->get("/r/(:num)", "Home::showRec/$1");

The controller code:

   $rid = $mymodel->mSaveNew($data);  ##return correct new id
   if ($rid === false) {
       $this->session->setFlashdata('errors', "?system error adding new record");
   } else {
       $this->session->setFlashdata('messages', "*** Added new record #$rid");
   }
   return redirect("/r/$rid");  ### this fails ###

What is wrong with my route?

The same redirect() route called in another controller method for updating an existing record works fine.

6
  • 1
    is $rid returned as expected? a number? Commented Jul 2 at 10:51
  • Yes it has a non-zero value; in my last test, "The route for '/r/88' cannot be found." Commented Jul 2 at 14:37
  • I should mention: This controller method is the one that handles an Add New request (saveNewRec). The routes are: $routes->get('/a', 'Home::addRec'); $routes->post('/a', 'Home::saveNewRec'); Commented Jul 2 at 14:44
  • 1
    have you tried this? return redirect()->to("/r/$rid"); Commented Jul 3 at 5:53
  • 1
    The redirect() function operates differently in CI4 which @shirshak007 comment above makes reference too. You are using the CI3 style but you need the ->to() bit as well in CI4.
    – Antony
    Commented Jul 4 at 19:02

0

Browse other questions tagged or ask your own question.