Text/html format in codeigniter rest_controller response

Active3 hr before
Viewed126 times

6 Answers


set rest default format in REST_Controller config file to html ,Making statements based on opinion; back them up with references or personal experience., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Find centralized, trusted content and collaborate around the technologies you use most.

set rest default format in REST_Controller config file to html

$config['rest_default_format'] = 'html';

Whenever a client (a web browser, smartphone app, etc) makes a request, it sends a small text message to the server and waits for a response.,In addition, the Response class allows you to work the HTTP cache layer for the best performance.,The first part of this chapter gives an overview. After the concepts are out of the way, we will discuss how to work with the requests and responses within CodeIgniter.,CodeIgniter also provides a Response class that is an object-oriented representation of the HTTP response. This gives you an easy and powerful way to construct your response to the client:

GET / HTTP / 1.1
Host codeigniter.com
Accept: text / html
User - Agent: Chrome / 46.0 .2490 .80
load more v

By giving both the API developer and the client application the choice of data formats to use, the API is opened up to a much wider audience and can be used with more programming languages and systems. These three are not the only formats supported, out of the box your REST API can use:, xml - almost any programming language can read XML,Keep an eye out for these differences as they can catch you out. If you get stuck, simply echo $this->rest->debug() for a whole range of information on your REST request.,Less commonly used and not supported by most browsers, PUT is used to create a new resource.

In the MVC pattern, a controller is the central point of the logic. It is called when a user makes a request and then based on the logic in the controller it fetches data and outputs views. CodeIgniter contains its own logic for how a Controller should work, but as we are doing something different we need our own REST_Controller library to contain its own REST related logic. So instead of simply using:

< ? php
class Example_api extends Controller {

load more v

The MIME type of the response data supported, which are JSON/XML/TEXT/HTML etc.,HTTP methods are mapped to CRUD (create, read, update and delete) actions for a resource. Although you can make slight modifications such as making the PUT method to create or update, the basic patterns are listed as follows.,Visual representation of the above test case:,Visual representation of the above added data:

Create a MySQL table contact under database roytuts. This table stores information for contact address details. For MySQL version 5.x, use the following table structure.

USE `roytuts`;

/*Table structure for table `contact` */


CREATE TABLE `contact`(
   int(10) unsigned NOT NULL AUTO_INCREMENT,
   varchar(30) COLLATE utf8_unicode_ci NOT NULL,
   varchar(150) COLLATE utf8_unicode_ci NOT NULL,
   varchar(15) COLLATE utf8_unicode_ci NOT NULL,
   PRIMARY KEY(`contact_id`)
) ENGINE = InnoDB AUTO_INCREMENT = 13 DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;
load more v

If you do not specify a parameter while calling to a web service a bad request is returned.,POST method is used to create a new resource. $this->post() method in CI is used to access values sent by client.,As there are no parameters; so click on Send button. Request is sent to server, in response all book records are returned as a JSON a object,In image below you can see  address bar for adding web service URL with a button to add parameters. Clicking on Send button, web service request is sent to server.

In order to create RESTful web services in CodeIgniter first, you have to install PHP, MySQL. If you have not installed PHP and MySql, then visit WAMP or XAMPP website, download and install PHP MySQL. After installation, open PhpMyAdmin. Click on SQL tab and execute queries given below.

--Database: `dbbookstore`

USE dbbookstore;

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

--Table structure
for table "tbl_books"

CREATE TABLE tbl_books(
   id int(11) NOT NULL,
   name varchar(500) NOT NULL,
   price float(8, 2) NOT NULL,
   author varchar(300) NOT NULL,
   category varchar(250) NOT NULL,
   language varchar(100) NOT NULL,
   ISBN varchar(40) NOT NULL,
   publish_date date NOT NULL

--Dumping data
for table "tbl_books"

INSERT INTO tbl_books(id, name, price, author, category, language, ISBN, publish_date)
   (1, 'Red Hat Enterprise Linux 6 Administration', 50.00, 'Sander van', 'Computer Science', 'en', '984-1234-12341234', '2013-12-05'),
   (2, 'Design Patterns: Elements of Reusable Object-Oriented Software ', 15.11, 'Ralph Johnson, John Vlissides, Grady Booch', 'Computer Science', 'en', '978-0201633610', '2016-03-01'),
   (3, 'Machine Learning for Absolute Beginners\r\n', 10.36, 'Oliver Theobald', 'Computer Science', 'en', '123-58679-654', '2016-08-01'),
   (4, 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming', 21.58, ' Eric Matthes', 'Programming', 'en', '659-8546-324', '2015-11-30'),
   (5, 'Data Structures and Algorithms in Java', 102.65, 'Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser', 'Computer Science', 'en', ' 978-1118777788', '2014-06-23'),
   (6, 'Star Wars: Darth Vader Vol. 1: Vader', 26.54, 'Kieron Gillen', 'Comic Novels', 'en', '485-6582-658', '2015-09-16'),
   (7, 'Star Wars Vol. 1: Skywalker Strikes', 16.23, 'Jason Aron', 'Novels', 'en', '159-7539-985', '2011-04-11'),
   (8, 'Phonics for Kindergarten, Grade K ', 6.32, 'Carson-Dellosa Publishing ', 'Education', 'en', '412-6548-7854', '2016-08-10'),
   (9, 'Astrophysics for People in a Hurry ', 9.95, 'Astrophysics for People in a Hurry ', 'Science', 'en', '654-71235-654', '2010-10-02'),
   (10, 'Let'
      's Review Algebra I', 8.54, 'Gary Rubinstein (Author) ', 'Science', 'en', '978-1438009854', '2006-03-24');

for dumped tables

for table `tbl_books`
ALTER TABLE tbl_books

for dumped tables

for table `tbl_books`
ALTER TABLE tbl_books
load more v

You can set format an array into either JSON or XML and set the content type header to the appropriate mime with the setJSON and setXML methods. Typically, you will send an array of data to be converted:,CodeIgniter\HTTP\Response,Sets the HTTP status code that should be sent with this response:,Sets the content type this response represents:

$this - > response - > setStatusCode(404) - > setBody($body);
load more v

Other "response-format" queries related to "Text/html format in codeigniter rest_controller response"