CodeIgniter Forums
Auto Routing (improved) post method not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Auto Routing (improved) post method not working (/showthread.php?tid=90559)



Auto Routing (improved) post method not working - anil.suthar - 04-02-2024

I'm using Auto Routing (Improved) and have set $autoRoute option to true in app/Config/Routing.php

and

$autoRoutesImproved to true in app/Config/Feature.php


I have created a Controller: Auth

class Auth extends BaseController
{
    public function postLoginUser()
    {
        var_dump($this->request->getPost());
        exit;
    }
}


I am posting data using HTML form to url = auth/loginUser

<form id="frmLogin" accept-charset="UTF-8" method="post" action="<?php echo get_url("auth/loginUser") ?>"
      role="form" data-parsley-validate>
    <input class="form-control form-control-border border-width-2 form-control-lg" placeholder="E-mail"
          title="Enter E-mail" name="email"
          id="email" data-parsley-trigger="change" required="" type="email">
    <input class="form-control form-control-border border-width-2 form-control-lg" placeholder="Password"
          title="Enter Password"
          name="password" type="password" value="" required>
    <button id='submit' type="submit" class="btn btn-lg btn-primary btn-block btn-flat">Submit</button>
</form>


On submit this form I'm getting error:

404
Controller or its method is not found: \App\Controllers\Auth::getLoginUser


Can anyone help me sort this issue?



RE: Auto Routing (improved) post method not working - kenjis - 04-02-2024

1. Check your routes.
See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#spark-routes

2. Check the requests with your browser's developer tools.
The error message shows that the request is a GET request,
because it says getLoginUser.


RE: Auto Routing (improved) post method not working - anil.suthar - 04-02-2024

Routes:
Code:
php spark routes
Code:
+------------+----------------+------+--------------------------------------+----------------+---------------+
Code:
| Method    | Route          | Name | Handler                              | Before Filters | After Filters |
+------------+----------------+------+--------------------------------------+----------------+---------------+
| GET        | /              | »    | \App\Controllers\Home::index        |                | toolbar      |
| GET(auto)  | auth/login    |      | \App\Controllers\Auth::getLogin      |                | toolbar      |
| POST(auto) | auth/loginUser |      | \App\Controllers\Auth::postLoginUser |                | toolbar      |
| GET(auto)  | payee          |      | \App\Controllers\Payee::getIndex    |                | toolbar      |
| GET(auto)  | payee/list/..  |      | \App\Controllers\Payee::getList      |                | toolbar      |
+------------+----------------+------+--------------------------------------+----------------+---------------+

Devtools:
[Image: 47218092-ce9c184a6f7deeda20ceb417b2e99a3...64264a0731]


RE: Auto Routing (improved) post method not working - anil.suthar - 04-03-2024

Found the solution...

You can not use trailing slash "/" at the end of the (action) URLs.

I replaced
Code:
http://cp.local/auth/loginUser/

to
Code:
http://cp.local/auth/loginUser

and it worked !



@kenjis  Shouldn't this be corrected at Codeigniter side?


RE: Auto Routing (improved) post method not working - kenjis - 04-03-2024

By default, public/.htaccess redirects requests with the trailing slash (and if the path is not a directory)
to the URI without the trailing stash. This is the cause of the GET request.

If you want to accept `auth/loginUser/`,
remove the redirect settings in public/.htaccess