CodeIgniter Forums
Routing issue with $routes->post - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Routing issue with $routes->post (/thread-74291.html)

Pages: 1 2


Routing issue with $routes->post - ogomez - 09-06-2019

Every time I try to post to the create news. CI is not finding the route. Am I doing something wrong? Tested this using postman.

Controller
PHP Code:
<?php namespace App\Controllers\api;
use 
CodeIgniter\API\ResponseTrait;
use 
App\Models\NewsModel;

class 
News extends \CodeIgniter\Controller {

 public function 
getNews($slug=null) {      
        $model 
= new NewsModel();
        if(strlen($slug) > 1) {
            $news $model->getNews($slug);  
        
}  else {
            $news $model->getNews();
        }
        $this->response->setHeader('Cache-Control''no-cache')
                      ->appendHeader('Cache-Control''must-revalidate')
                      ->setContentType('application/json');
        if($news) {
            return $this->respond($news);
        }
        else {
          return $this->fail('No records returned');
        }
    }

    public function createNews() {
        
        $model 
= new NewsModel();

        if($this->validate([
            'title' => 'required|min_length[3]|max_length[255]',
            'body'  => 'required',
                            ]) ) {
        $model->save([
            'title' => $this->request->getPost('title'),
            'slug'  => url_title($this->request->getPost('title')),
            'body'  => $this->request->getVar('body')
            ]);
            $data['result'] = "Records has been created";
            return  $this->respondCreated($data);
        }  else {
            $data['errors'] = \Config\Services::validation()->getErrors();
            return $this->fail($data);
        }
    }



Routes
PHP Code:
$routes->get('api/news''App\Controllers\api\News::getNews');
$routes->get('api/news/(:segment)''App\Controllers\api\News::getNews/$1');
$routes->post('api/news''App\Controllers\api\News::createNews'); 

Thanks in advance


RE: Routing issue with $routes->post - InsiteFX - 09-07-2019

Did you try using route groups?

PHP Code:
$routes->group('api', ['namespace' => 'App\Controllers\api'], function($routes)
    $routes->get('news''News::getNews');
    $routes->get('news/(:segment)''News::getNews/$1');
    $routes->post('news''News::createNews');
}); 

Remember you need to use the correct namespace also.


RE: Routing issue with $routes->post - ogomez - 09-09-2019

Same result, I do have the namespace correct on my routes although the grouping looks much nicer. The routing is still incorrect.

PHP Code:
$routes->post('api/news''App\Controllers\api\News::createNews'); 

Thanks


RE: Routing issue with $routes->post - includebeer - 09-10-2019

PHP Code:
namespace App\Controllers\api

Is you News controller in the 'api' subdirectory?


RE: Routing issue with $routes->post - ogomez - 09-10-2019

(09-10-2019, 02:47 AM)includebeer Wrote:
PHP Code:
namespace App\Controllers\api

Is you News controller in the 'api' subdirectory?

Yes


RE: Routing issue with $routes->post - includebeer - 09-10-2019

Are you sure the form is posted to the right URL? Show your opening form tag.


RE: Routing issue with $routes->post - ogomez - 09-10-2019

(09-10-2019, 08:16 AM)includebeer Wrote: Are you sure the form is posted to the right URL? Show your opening form tag.

I'm using it as an API end point. Testing it with postman. Sending a post request.

controllers/api/news.php
Controller
PHP Code:
<?php namespace App\Controllers\api;

use 
CodeIgniter\RESTful\ResourceController;
use 
CodeIgniter\API\ResponseTrait;
use 
App\Models\NewsModel;

class 
News extends \CodeIgniter\RESTful\ResourceController {
    use ResponseTrait;
    public $supportedResponseFormats = [
        'application/json'
    ];

    public $formatters = [
        'application/json' => \CodeIgniter\Format\JSONFormatter::class
    ];

    function __construct() {
      
    
    
}

    public function index($slug=null) {      
        $model 
= new NewsModel();
        helper('form');
        if(strlen($slug) > 1) {
            $news $model->getNews($slug);  
        
}  else {
            $news $model->getNews();
        }
        $this->response->setHeader('Cache-Control''no-cache')
                      ->appendHeader('Cache-Control''must-revalidate')
                      ->setContentType('application/json');
        if($news) {
            return $this->respond($news);
        }
        else {
          return $this->fail('No records returned');
        }
    }

    public function update($id NULL) {
        echo 'hi';
    }

    public function create() {
        
        $model 
= new NewsModel();

        if($this->validate([
            'title' => 'required|min_length[3]|max_length[255]',
            'body'  => 'required',
                            ]) ) {
        $model->save([
            'title' => $this->request->getPost('title'),
            'slug'  => url_title($this->request->getPost('title')),
            'body'  => $this->request->getVar('body')
            ]);
            $data['result'] = "Records has been created";
            return  $this->respondCreated($data);
        }  else {
            $data['errors'] = \Config\Services::validation()->getErrors();
            return $this->fail($data);
        }
    }

    public function delete($id NULL) {
        echo $id;
    }


Route:
PHP Code:
$routes->group('api', ['namespace' => 'App\Controllers\api'], function($routes) {
 
$routes->resource('news');
}); 



RE: Routing issue with $routes->post - includebeer - 09-10-2019

(09-10-2019, 09:23 AM)ogomez Wrote:
(09-10-2019, 08:16 AM)includebeer Wrote: Are you sure the form is posted to the right URL? Show your opening form tag.

I'm using it as an API end point. Testing it with postman. Sending a post request.

It doesn't matter if it's an html form or if you use postman. At what URL do you post the data? If CI doesn't find the route, maybe that route isn't configured at all or is misconfigured or you have an error in your post url.


RE: Routing issue with $routes->post - ogomez - 09-10-2019

(09-10-2019, 10:09 AM)includebeer Wrote:
(09-10-2019, 09:23 AM)ogomez Wrote:
(09-10-2019, 08:16 AM)includebeer Wrote: Are you sure the form is posted to the right URL? Show your opening form tag.

I'm using it as an API end point. Testing it with postman. Sending a post request.

It doesn't matter if it's an html form or if you use postman. At what URL do you post the data? If CI doesn't find the route, maybe that route isn't configured at all or is misconfigured or you have an error in your post url.

The route is configured correctly and I'm not seeing any errors on the logs.


RE: Routing issue with $routes->post - includebeer - 09-11-2019

Ok, fine. If you don't want to give more info, then there's nothing more I can do.