Welcome Guest, Not a member yet? Register   Sign In
route cannot be found while reverse-routing.
#1

Hi.. i get the error mentioned in the title when i try to update a row in db. And the validation failes.
Routes.php In a group admin
Code:
//Kategorier
    $routes->get('cat/list', 'CatController::index');
    $routes->get('cat/create', 'CatController::create');
    $routes->post('cat/store', 'CatController::store');
    $routes->get('cat/edit/(:num)', 'CatController::edit/$1');
    $routes->post('cat/update/(:num)', 'CatController::update/$1');
    $routes->get('cat/delete/(:num)', 'CatController::delete/$1');

in the controller
Code:
## Validation
          $input = $this->validate([
            'kategori' => 'required|min_length[3]',
          ]);

          if (!$input) {
             return redirect()->route('admin/cat/edit/'.$id)->withInput()->with('validation',$this->validator);
          } else {

error message from debug bar
Code:
          ## Validation
83           $input = $this->validate([
84             'kategori' => 'required|min_length[3]',
85           ]);
86   
87           if (!$input) {
88              return redirect()->route('admin/cat/edit/'.$id)->withInput()->with('validation',$this->validator);
89           } else {

With line 88 marked.
Is there enough info to answer?
Rgrds Bengt
Reply
#2

You have no admin/cat/edit/ route did you mean 'cat/edit/'.$id
What did you Try? What did you Get? What did you Expect?

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

(05-19-2021, 08:42 PM)InsiteFX Wrote: You have no admin/cat/edit/ route did you mean 'cat/edit/'.$id

the route is in a route group...
PHP Code:
// Admin routes
$routes->group("admin", ["filter" => "auth"], function ($routes) {
    $routes->get("/""home::index");
    
$routes->get('listusers''User::list', ['filter' => 'auth']);
    
$routes->get('viewuser''User::view', ['filter' => 'auth']);

    
//Kategorier
    
$routes->get('cat/list''CatController::index');
    
$routes->get('cat/create''CatController::create');
    
$routes->post('cat/store''CatController::store');
    
$routes->get('cat/edit/(:num)''CatController::edit/$1');
    
$routes->post('cat/update/(:num)''CatController::update/$1');
    
$routes->get('cat/delete/(:num)''CatController::delete/$1'); 

When typing the adress: localhost/admin/cat/edit/1 it works fine. Its just when i try to edit and it fails on validation i get this error.
If i try to create a new using admin/cat/create it works fine... so the code below works fine...
PHP Code:
       ## Validation
        
   $input $this->validate([
            
  'kategori' => 'required|min_length[3]|is_unique[kategori.kategori]',
        
   ],
        
   // ERRORS
            
   'kategori' => [    'min_length' => 'Kategori må inneholde minst 3 bokstaver',
            
                       'is_unique' => 'Denne kategorien finnes allerede']
            ]);
  
           
if (!$input) {
            
  return redirect()->route('admin/cat/create')->withInput()->with('validation',$this->validator); 
        
   } else { 
Reply
#4

(This post was last modified: 05-20-2021, 12:24 AM by iRedds.)

PHP Code:
//1. Named route
$routes->get('cat/edit/(:num)''CatController::edit/$1', ['as' => 'cat.edit']);

redirect()->route('cat.edit', [$id])

//2. Controller&method (with namespace)
redirect()->route('\App\Controllers\CatController::edit', [$id])

//3. Url
redirect()->to('admin/cat/edit/' $id

Note that the withInput() method already adds validation error data.
To get errors you need:
PHP Code:
// init session (Even though it looks stupid in terms of practicality, but otherwise will not work.)
session();
// get validator instance
$validation Services::validation();
// get errors 
$validation->getErrors(); 
Reply
#5

Just pussled with the fact that it works under function store() but not in update() in the same controller.

Here is the full CatController.php

Line 38: Works fine
Line 91: Fails

PHP Code:
<?php

namespace App\Controllers;

use 
App\Controllers\BaseController;
use 
App\Models\RecipeCatModel;

class 
CatController extends BaseController
{
    public function 
index(){
        
$subjects = new RecipeCatModel();
  
        
## Fetch all records
        
$data['subjects'] = $subjects->findAll();
        return 
view('admin/cat/index',$data);
     }
  
     
public function create(){
        return 
view('admin/cat/create');
     }
  
     
public function store(){
        
$request service('request');
        
$postData $request->getPost();
  
        
if(isset($postData['submit'])){
  
           
## Validation
        
   $input $this->validate([
            
  'kategori' => 'required|min_length[3]|is_unique[kategori.kategori]',  
           
],
        
   // ERRORS
            
   'kategori' => [    'min_length' => 'Kategori må inneholde minst 3 bokstaver',
            
                       'is_unique' => 'Denne kategorien finnes allerede']
            ]);
  
           
if (!$input) {
            
  return redirect()->route('admin/cat/create')->withInput()->with('validation',$this->validator);   //THIS WORKS JUST FINE
        
   } else {
  
              $subjects 
= new RecipeCatModel();
  
              $data 
= [
                 
'kategori' => $postData['kategori'],
            
  ];
  
              
## Insert Record
            
  if($subjects->insert($data)){
                 
session()->setFlashdata('message''Added Successfully!');
                 
session()->setFlashdata('alert-class''alert-success');
  
                 
return redirect()->route('admin/cat/create'); 
            
  }else{
                 
session()->setFlashdata('message''Data not saved!');
                 
session()->setFlashdata('alert-class''alert-danger');
  
                 
return redirect()->route('admin/cat/create')->withInput(); 
            
  }
  
           
}
        }
  
     
}
  
     
public function edit($id 0){
  
        
## Select record by id
        
$subjects = new RecipeCatModel();
        
$subject $subjects->find($id);
  
        $data
['subject'] = $subject;
        return 
view('admin/cat/edit',$data);
  
     
}
  
     
public function update($id 0){
        
$request service('request');
        
$postData $request->getPost();
  
        
if(isset($postData['submit'])){
  
          
## Validation
        
  $input $this->validate([
            
'kategori' => 'required|min_length[3]',
         ],
         [ 
// ERRORS
             
'kategori' => [    'min_length' => 'Kategori må inneholde minst 3 bokstaver']
        
  ]);
  
          
if (!$input) {
            return 
redirect()->route('admin/cat/edit/'.$id)->withInput()->with('validation',$this->validator); //THIS ONE FAILES
            
        
  } else {
  
             $subjects 
= new RecipeCatModel();
  
             $data 
= [
                
'kategori' => $postData['kategori'],
             ];
  
             
## Update record
             
if($subjects->update($id,$data)){
                
session()->setFlashdata('message''Updated Successfully!');
                
session()->setFlashdata('alert-class''alert-success');
  
                
return redirect()->route('admin/cat/list'); 
             }else{
                
session()->setFlashdata('message''Data not saved!');
                
session()->setFlashdata('alert-class''alert-danger');
  
                
return redirect()->route('cat/edit/'.$id)->withInput(); 
             }
  
          
}
        }
  
     
}
  
     
public function delete($id=0){
  
        $subjects 
= new RecipeCatModel();
  
        
## Check record
        
if($subjects->find($id)){
  
           
## Delete record
        
   $subjects->delete($id);
  
           session
()->setFlashdata('message''Deleted Successfully!');
        
   session()->setFlashdata('alert-class''alert-success');
        }else{
        
   session()->setFlashdata('message''Record not found!');
        
   session()->setFlashdata('alert-class''alert-danger');
        }
  
        
return redirect()->route('admin/cat/list');
  
     
}

Reply
#6

Because the route is not 'admin/cat/edit/1', but 'admin/cat/edit/([0-9]+)'
Reply
#7

(05-20-2021, 04:49 AM)iRedds Wrote: Because the route is not 'admin/cat/edit/1', but 'admin/cat/edit/([0-9]+)'
Not sure if I understand... Is there something i can do to fix it ?
Reply
#8

(05-20-2021, 05:16 AM)bengtdg Wrote:
(05-20-2021, 04:49 AM)iRedds Wrote: Because the route is not 'admin/cat/edit/1', but 'admin/cat/edit/([0-9]+)'
Not sure if I understand... Is there something i can do to fix it ?
Fix what? Everything works fine.
I showed above how to get out of the situation in three ways.

But if you want to change behavior, you can offer your PR.
Reply
#9

(05-20-2021, 08:54 AM)iRedds Wrote: Fix what? Everything works fine.
I showed above how to get out of the situation in three ways.

But if you want to change behavior, you can offer your PR.
Me hust beeing a bit slow Smile
Changed it to 

PHP Code:
return redirect()->to('/admin/cat/edit/'.$id)->withInput()->with('validation',$this->validator); 

Thanx for helping me out Big Grin
Rgrds Bengt
Reply




Theme © iAndrew 2016 - Forum software by © MyBB