codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.35k stars 1.9k forks source link

RESTful behaviour #2122

Closed psuplat closed 5 years ago

psuplat commented 5 years ago

First of all I apologize for throwing a "help me" question here instead of on the forum, but since the registration is temporarily disabled I can't create an account to post.

I trying to understand how the HTTP verbs in routes function.

I have a route defined as such: $routes->post('/users/create', 'Users::create');

and my controller is as follows:

<?php namespace App\Controllers;

use App\Models\UsersModel;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\Request;

class Users extends BaseController
{
    use ResponseTrait;

    public function create()
    {
            // Some code in here
            return $this->respond($data, 200);
        }
}

now, if I try to access the route with GET method I would expect to see some kind of error, probably 405 - Method not allowed. Instead the code executes and I get a 200 response.

Am I missing something obvious here?

MGatner commented 5 years ago

If you didn’t turn off autorouting then users/create will still match to Users::create without any explicit routes.

jim-parry commented 5 years ago

$routes->post('/users/create', 'Users::create'); will add a route. Any "get" route that would resolve normally still will, so your "200" is expected. You can ask questions on the slack channel until the forum registration has been restored.