PHP CodeIgniter 4 -表单提交-错误“找不到'registrations/index'的路径”

u59ebvdq  于 2022-12-06  发布在  PHP
关注(0)|答案(2)|浏览(97)

我在视图中使用form_open() helper方法来指示控制器方法处理表单提交操作,并在app\Config\Routes.php中定义路由。
我仍然收到错误Can't find a route for 'registrations/index'
请协助解决此问题。下面提供了代码片段。

错误:

register.php视图文件:
<?php echo form_open('/registrations/index'); ?>
Registrations.php控制器:
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');
carvr3hs

carvr3hs1#

form_open('/registrations/index')
解释:

上面的代码行将创建一个表单,该表单指向您的站点URL加上“/registrations/index”URI段,如下所示:

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

如果仔细看一下上面自动生成的form开始标记,这实际上是一个POST HTTP请求,但是您在app\Config\Routes.php文件中使用->get(...)方法定义了路由。

解决方案:

代替(Routes.php):

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

使用这个:

$routes->post('/registrations/index', 'Registrations::index');
  • 请注意->post(...)的用法。*

资源:Form Helper

8i9zcol2

8i9zcol22#

我认为您的AutoRoutes默认设置为false。
将此行添加到Routes.php

$routes->setAutoRoute(true);

相关问题