Welcome Guest, Not a member yet? Register   Sign In
Routing issue with $routes->post
#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


Messages In This Thread
Routing issue with $routes->post - by ogomez - 09-06-2019, 08:50 AM
RE: Routing issue with $routes->post - by ogomez - 09-09-2019, 02:53 PM
RE: Routing issue with $routes->post - by ogomez - 09-10-2019, 07:07 AM
RE: Routing issue with $routes->post - by ogomez - 09-10-2019, 09:23 AM
RE: Routing issue with $routes->post - by ogomez - 09-10-2019, 12:49 PM
RE: Routing issue with $routes->post - by ogomez - 09-11-2019, 06:10 PM



Theme © iAndrew 2016 - Forum software by © MyBB