I've read the following topics/tutorials:
Codeigniter RESTful API Server
http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814
https://github.com/chriskacerguis/codeigniter-restserver
And I still can't figure out why I'm with route problems and XML problems.
My webservice controller is in the folder controllers/api/webservice.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'/libraries/RESTful/REST_Controller.php';
class webservice extends REST_Controller{
function get() {
$data = array();
$data['name'] = "TESTNAME";
$this->response($data);
}
}
In the tutorials there is no need to add routes, and in order to receive the XML page error I needed to add the following route or it wouldn't work:
$route['api/webservice/get'] = 'api/webservice/get';
My structure folder of codeIgniter:
> application
> config
rest.php (did not change anything from the GitHub download)
> controllers
> api
webservice.php
key.php
> libraries
> RESTful
REST_Controller.php (changed line 206 to: $this->load->library('RESTful/format');)
format.php
From the tutorial, the following link works without routes:
http://localhost/testRestful/index.php/api/example/users
Mine only works with route
http://localhost/myproject/index.php/api/webservice/get
And I receive the following error:
It does not say anything else. I can't figure out which file is the error talking about.
you cannot write get function if you use REST_Controller.
give that function name test_get.
class webservice extends REST_Controller{
function test_get() {
$data = array();
$data['name'] = "TESTNAME";
$this->response($data);
}
}
Now you can access the page with this link
http://localhost/myproject/index.php/api/webservice/test
_get and _post is added end of the function to detect either it is get request or post requerst.
Related
I'm implementing the api in codeigniter using the restcontroller and format files shown in the gist below, once inserted I implement the Api.php file in the path specified below but when I try to call the api as a test I get the following error 404 not found, what is it and how can I fix this?
url to call api get: http://localhost/api/Api/testget
I have this file (rest controller and format)
https://gist.github.com/riccardopirani/8878fcda16cae2ad45e4b1c9e624f619
Php Code(in folder /controllers/api/Api.php:
<?php
use Restserver\Libraries\REST_Controller;
require(APPPATH.'libraries/REST_Controller.php');
class Books extends REST_Controller {
function testget()
{
$data = array("message"=>"RESTfull API sample");
$this->response($data);
}
}
Write method name as test_get. Keep class and file name same, then call (get) http://localhost/[projectname]/index.php/api/book/test
Ok, guys, this is the case...
I am working in an old porject made it with CodeIgniter v2 (currently all is in localhost). For new features I created a folder called v1 inside the api folder
The structure of the project:
controllers
api
v1
visit.php
orders.php
controller1.php
controller2.php
The problem is that I can not access to the visit.php controller
to test purposes I set the visit controller in the api folder an access it whit this:
localhost/projectname/index.php/api/visit/visits
visits is the function in the visit controller
With this way everything works!! but, when I set the visit controller in the v1 folder I get a 404 page not found error.
localhost/projectname/index.php/api/v1/visit/visits
Extra
Another think that have keep in mind is. This project is using a library to the REST API so, in the visit controller are tho functions
public function visits_get(){
// return an arrays of visits
}
public function visits_post(){
// to add a new visit in a bd
}
So, the function will be called depends on the request method
I have been reading and I found that I have to configure the route.php, actually I did it but without success.
Thanks and I hope you understand what I am asking!
ROUTE.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "welcome";
$route['404_override'] = '';
This is all that the route.php has in it ._.
To my knowledge, any controllers falling outside the application/controllers/controller_name.php naming convention need to be explicitly defined inside the routes.php file, otherwise CI will not look inside subfolders. It's not much of a problem, actually, add something like this for your controllers:
//You'll need to do this for all of your API controllers, unfortunately
$route['api/v1/(:any)'] = 'api/v1/$1';
//If you have controllers taking arguments, eg. /api/v1/stuff/1
$route['api/v1/(:any)/(:any)'] = 'api/v1/$1/$2';
//Catch-all route for 404's, recommended
$route['api/(:any)'] = 'api/v1/error_api';
Have a look at the routing docs for CodeIgniter 2 for more info.
frontend of my CakePHP app is based on the REST API, each controller has user and admin methods.
I would like to run admin method on controller with classic Cake view.
But if i type into browser URL like this:
http://myproject.loc/admin/cards/add/
I get only:
{
code: 500,
name: "View file api/app/View/Cards/json/admin_add.ctp" is missing.",
message: "View file api/app/View/Cards/json/admin_add.ctp" is missing.",
url: "/admin/cards/add/"
}
Question is:
How can i set right URL for admin routes? (without json prefix)
Read Json and XML Views in CakePHP and read it properly. A common mistake is that people forget to add the RequestHandler component to their components array. The component will take care of getting the right layout set for you and the data serialized.
If you really want to insist on removing the .json suffix from the URL but calling the same URL via browser and AJAX you'll have to make sure that you request the right content type. Check the headers you send.
I'm not sure if the RequestHandler will send you the right response if you only request json (application/json) in the header without using the suffix in the URL, it should work, give it a try.
Here is the ref.
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html
You need to add following line into the your controller function
I hope your encoding it to the json in the following way
$arrayData = array(...);//as example
class PostsController extends AppController {
public function index() {
$this->autoRender = false;
$this->layout = null;
$this->set(compact('posts', 'comments'));
}
}
// View code - app/View/Posts/json/index.ctp
foreach ($posts as &$post) {
unset($post['Post']['generated_html']);
}
echo json_encode(compact('posts', 'comments'));
I am new to codeigniter and really interested in trying it out. I followed the guide but ran into a problem. It seems like I am unable to load my first page properly.
I have inside the view folder another folder called general and inside it index.php.
in controller folder, I have sub-folder with general and inside it is Default controller.
I have the following route and yet the page showing is blank
$route['default_controller'] = "general/default";
$route['404_override'] = '';
When I visit the link, I type this in browser:
http://localhost:8888/treventa/
and the screen is blank. Am I doing something wrong? Sorry if this is too simple but a person got to learn from his mistake :)
Try with me step by step:
Firstly: the Controller:
(main.php) File content
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Main extends CI_Controller {
public function index() {
$this->load->view('general/welcome');
}
}
Secondly: The view:
(welcome.php) File content
You can put anything you want
<h1> Hello, I'm the view file </h1>
Finaly: The routes:
(routes.php) File content
$route['default_controller'] = "general/main";
Now call the script like this http://localhost/codeIgniter/, that's where codeIgniter is the script folder name.
I think everything now is clear.
CI trying to use method default() of general controller. If You want to use folders for controllers, make sure You specify method, so try $route['default_controller'] = "general/default/index";
And one more thing: this method (default_controller) will be used when You trying reach Your root http://localhost:8888/, if You want to route request from http://localhost:8888/treventa/ to default controller, You need to add route-rule to /config/routes.php something like $route['treventa'] = "general/main/index";
I have created a fully functional CakePHP web application. Now, i wanna get it to the next level and make my app more "opened". Thus i want to create an RESTful API. In the CakePHP docs,i found this link (http://book.cakephp.org/2.0/en/development/rest.html) which describes the way to make your app RESTful. I added in the routes.php the the two lines needed,as noted at the top of the link,and now i want to test it.
I have an UsersController.php controller,where there is a function add(),which adds new users to databases. But when i try to browse under mydomain.com/users.json (even with HTTP POST),this returns me the webpage,and not a json formatted page,as i was expecting .
Now i think that i have done something wrong or i have not understood something correctly. What's actually happening,can you help me a bit around here?
Thank you in advace!
You need to parse JSON extensions. So in your routes.php file add:
Router::parseExtensions('json');
So a URL like http://example.com/news/index you can now access like http://example.com/news/index.json.
To actually return JSON though, you need to amend your controller methods. You need to set a _serialize key in your action, so your news controller could look like this:
<?php
class NewsController extends AppController {
public function index() {
$news = $this->paginate('News');
$this->set('news', $news);
$this->set('_serialize', array('news'));
}
}
That’s the basics. For POST requests, you’ll want to use the RequestHandler component. Add it to your controller:
<?php
class NewsController extends AppController {
public $components = array(
'RequestHandler',
...
);
And then you can check if an incoming request used the .json file extension:
public function add() {
if ($this->RequestHandler->extension == 'json') {
// JSON request
} else {
// do things as normal
}
}