Yii Framework Forum: Beginners Guide To Yii2 RestAPI - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Beginners Guide To Yii2 RestAPI How To Setup A Restful Application For Beginners Rate Topic: ***** 1 Votes

#1 User is offline   auzadventure 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 20-August 15
  • Location:Singapore

Posted 02 June 2017 - 01:11 AM

So you want to setup a Rest Application Using Yii2

What is REST

Basically you input something like
yousite/companies/index 


into a url and get back
[ 'name':'wesvault','name':'yii2 company']


that is really all there is to REST. It gets things.

Step 1:

Install Yii2 Framework. Lots of examples on how to do this. I recommend the advanced template or the modified yii2-practical template

Step 2:

Create a Model.
Go to gii , model generator and input your table "companies" at

common\models\companies


Step 3:

Create A controller

Same thing in Gii, controller generator

name: CompaniesController
path: frontend\controller\CompaniesController

Step 4:
Edit this controller to use the restful add-ons



namespace frontend\controllers;

use yii\rest\ActiveController;    <--- This says use the rest version of activecontroller 

class ContactsController extends ActiveController   <--- This extends Activecontroller 
{
    
	public $modelClass = 'common\models\Contacts';  <-- you need to tell it which model class
	


}


Step 5:
Ok now it knows to return a restful action, but how does it handle the incoming url instead of a normal web display.
For this you need to setup the Url Manager, find it in

common/config/main.php


Add this to the Url Manager

 'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing'=> true,   <--- It must match GET , POST etc etc (any one of the rules) 
			
            'showScriptName' => false,
			'rules'=> [
				['class'=>'yii\rest\UrlRule',  <-- this is the standard rule class
				 'controller'=>'contacts',     <-- which controller   "contacts/{rule}"        
				 'extraPatterns'=>['GET hello' => 'hello',]   <-- I add a custom rule  
				 
				 ]
			]
		],


Step 6:
Give it a test.
Download POSTMAN or http://restclient.net/ for your browser then enter the url GET : yousite/contacts
you should see a return of some JSON objects. That's basically it.

Step 7: (custom actions rules)
Ok you have whatever yii2 gives you. But what if you want another rule action like say hello. Above you see I added the rule pattern

Just go to your controller and add in that action normally

public function actionHello()
	{
        return 'hello';
	}


Fire up your browser and try get yoursite/companies/hello. You should see "hello" come back.
Questions are the Answer.

Wesvault - Premium Economy Software
Yii2 Framework Specialist
http://www.wesvault.com

Singapore
1

#2 User is offline   Teffte 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 13-October 17

Posted 23 October 2017 - 01:42 PM

Thanks for the guide. Helps beginners like me a lot.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users