简体   繁体   English

Codeigniter 4 表单提交后出现错误 404 路由有问题

[英]Codeigniter 4 error 404 after form post submit there is problem with route

I using Codeigniter 4我使用 Codeigniter 4

I already add in Routes.php我已经添加了 Routes.php

$routes->get('/register', 'Auth::register');

and this is Auth.php这是Auth.php

    public function register()
    {
        $data['page_title']   = "Register";
        if ($this->request->getMethod() === 'post') {
            $email = $this->request->getPost('email');
            if ($this->validate([
                'email' => 'required',
                'pwd'  => 'required',
                'repwd' => 'required|matches[pwd]'
            ])) {
                $pwd = $this->request->getPost('pwd');
                $repwd = $this->request->getPost('repwd');
                $ip = $this->request->getIPAddress();
                return redirect()->to('/');
            }
            $data['email'] = $email;
            $this->session->setFlashdata('isFormError', true);
        }

        return view('auth/register', $data);
    }

for view is just simple html form with method post.视图只是简单的 html 表格和方法帖子。 after form submit, I got error:表单提交后,我收到错误:

404 - File Not Found
Controller or its method is not found: \App\Controllers\Register::index

I using http://localhost:8080/register and also redirected to http://localhost:8080/register but it load \App\Controllers\Register::index not \App\Controllers\Auth::register我使用http://localhost:8080/register并且还重定向到http://localhost:8080/register但它加载\App\Controllers\Register::index not \App\Controllers\Auth::register

how to solve this?如何解决这个问题? may be I miss something or is this CI4 bugs?可能是我错过了什么还是这是 CI4 错误?

I using Codeigniter 4我使用Codeigniter 4

I already add in Routes.php我已经在Routes.php中添加了

$routes->get('/register', 'Auth::register');

and this is Auth.php这是Auth.php

    public function register()
    {
        $data['page_title']   = "Register";
        if ($this->request->getMethod() === 'post') {
            $email = $this->request->getPost('email');
            if ($this->validate([
                'email' => 'required',
                'pwd'  => 'required',
                'repwd' => 'required|matches[pwd]'
            ])) {
                $pwd = $this->request->getPost('pwd');
                $repwd = $this->request->getPost('repwd');
                $ip = $this->request->getIPAddress();
                return redirect()->to('/');
            }
            $data['email'] = $email;
            $this->session->setFlashdata('isFormError', true);
        }

        return view('auth/register', $data);
    }

for view is just simple html form with method post.用于查看的只是带有方法post的简单html表单。 after form submit, I got error :表单提交后,我得到了错误:

404 - File Not Found
Controller or its method is not found: \App\Controllers\Register::index

I using http://localhost:8080/register and also redirected to http://localhost:8080/register but it load \\App\\Controllers\\Register::index not \\App\\Controllers\\Auth::register我使用http://localhost:8080/register并也重定向到http://localhost:8080/register但是它加载\\App\\Controllers\\Register::index而不是\\App\\Controllers\\Auth::register

how to solve this?怎么解决呢? may be I miss something or is this CI4 bugs?可能是我想念某些东西还是这是CI4错误?

I had a similar problem and managed to solve it.我有一个类似的问题并设法解决它。 It turns out that the problem lies in the request url Here is my Routes.php原来问题出在请求 url Here is my Routes.php

$routes->post('path', 'Mycontroller::method');
$routes->post('path/(:segment)', 'Mycontroller::method/$1');

When I make post request to localhost/path/ it return 404. But when I make post request to localhost/path (without trailing slash), it returns expected result.当我向 localhost/path/ 发出 post 请求时,它返回 404。但是当我向 localhost/path 发出 post 请求(不带斜杠)时,它返回预期的结果。 At first I think it because second route with segment, but it turns out after I delete the second route I still get the same result.起初我认为这是因为第二条路线有段,但事实证明,在我删除第二条路线后,我仍然得到相同的结果。

The "trailing slash in url problem" doesn't affect GET request “url 问题中的斜杠”不影响 GET 请求

So I just remove all trailing slash on every post request所以我只是删除每个帖子请求上的所有尾随斜杠

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM