Welcome Guest, Not a member yet? Register   Sign In
Routing issue with $routes->post
#1

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

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 09-09-2019, 02:53 PM by ogomez.)

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

PHP Code:
namespace App\Controllers\api

Is you News controller in the 'api' subdirectory?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

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

Is you News controller in the 'api' subdirectory?

Yes
Reply
#6

Are you sure the form is posted to the right URL? Show your opening form tag.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#7

(This post was last modified: 09-10-2019, 09:26 AM by ogomez.)

(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');
}); 
Reply
#8

(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.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#9

(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.
Reply
#10

Ok, fine. If you don't want to give more info, then there's nothing more I can do.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB