Welcome Guest, Not a member yet? Register   Sign In
Auto Routing (improved) post method not working
#1

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?
Reply
#2

(This post was last modified: 04-02-2024, 04:36 PM by kenjis.)

1. Check your routes.
See https://codeigniter4.github.io/CodeIgnit...ark-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.
Reply
#3

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]
Reply
#4

(This post was last modified: 04-03-2024, 12:34 AM by anil.suthar.)

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?
Reply
#5

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB