4

I'm using the form_open() helper method within the view to indicate the controller method to handle the form submission action and have the route defined in app\Config\Routes.php.

I'm still getting an error Can't find a route for 'registrations/index'.

Please assist with the issue. The code snippets are provided below.

Error:

enter image description here

register.php view file:

            <?php echo form_open('/registrations/index'); ?>

Registrations.php controller:

class Registrations extends BaseController {

    public function index() {

        $data['coursename'] = $this->getCourseName();  

        log_message('info','name field >' . $this->request->getVar('iname') . '<<');

        echo view('templates/header');
        echo view('pages/register', $data);
        echo view('templates/footer');
    }

Routes.php

$routes->get('/registrations/index', 'Registrations::index');
steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34
Siva
  • 61
  • 1
  • 1
  • 3

2 Answers2

9

I think your AutoRoutes is set to false by default.

add this line in your Routes.php

$routes->setAutoRoute(true);
Fikri Am
  • 91
  • 1
  • 3
  • there is a comment in Routes.php that mentioned `The Auto Routing (Legacy) is very dangerous`. If you want to enable it, don't forget to set `$autoRoutesImproved = true;` in app/Feature.php . Not sure why is the improved one not set to `true` by default – Anggara Jul 15 '23 at 18:14
6
form_open('/registrations/index')

Explanation:

The above line of code would create a form that points to your site URL plus the “/registrations/index” URI segments, like this:

<form action="http://your-site-domain.com/index.php/registrations/index" method="post" accept-charset="utf-8">

If you look hard enough at the auto-generated opening form tag above, this will essentially make a POST HTTP request. Yet you defined your route in the app\Config\Routes.php file using a ->get(...) method.

Solution:

Instead of (Routes.php):❌

$routes->get('/registrations/index', 'Registrations::index');

Use this: ✅

$routes->post('/registrations/index', 'Registrations::index');

Notice the use of ->post(...).

Resource: Form Helper.

steven7mwesigwa
  • 5,701
  • 3
  • 20
  • 34