-
ogomez
Junior Member
-
Posts: 26
Threads: 5
Joined: Oct 2015
Reputation:
0
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
-
InsiteFX
Super Moderator
-
Posts: 6,575
Threads: 331
Joined: Oct 2014
Reputation:
240
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 )
-
ogomez
Junior Member
-
Posts: 26
Threads: 5
Joined: Oct 2015
Reputation:
0
09-09-2019, 02:53 PM
(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
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
PHP Code: namespace App\Controllers\api;
Is you News controller in the 'api' subdirectory?
-
ogomez
Junior Member
-
Posts: 26
Threads: 5
Joined: Oct 2015
Reputation:
0
(09-10-2019, 02:47 AM)includebeer Wrote: PHP Code: namespace App\Controllers\api;
Is you News controller in the 'api' subdirectory?
Yes
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
Are you sure the form is posted to the right URL? Show your opening form tag.
-
ogomez
Junior Member
-
Posts: 26
Threads: 5
Joined: Oct 2015
Reputation:
0
09-10-2019, 09:23 AM
(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'); });
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
(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.
-
includebeer
CodeIgniter Team
-
Posts: 1,018
Threads: 18
Joined: Oct 2014
Reputation:
40
Ok, fine. If you don't want to give more info, then there's nothing more I can do.
|