Coder Social home page Coder Social logo

yii2-advanced-api's Introduction

RESTful API in Yii 2 Advanced Application Template

Yii2 Advanced Application Template with RESTful API configured. Take a look at http://budiirawan.com/setup-restful-api-yii2/ for more detail explanation

Install Composer Packages

You need Composer installed first.

composer self-update
composer install

Run Yii Init

Open terminal and go to the project folder and run

Mac/Linux

php ./init

Windows

init

Choose development environment and finish the steps.

Configure Database

create your database and configure it in common/config/main-local.php

Run Database Migration

This command will create new country table and populate its data

./yii migrate

Enable Mod Rewrite if you use Apache

Make sure you already enable this mod. Follow this Tutorial

yii2-advanced-api's People

Contributors

deerawan avatar joy007-py avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yii2-advanced-api's Issues

404 Not Found

nginx api

server {
    charset utf-8;
    client_max_body_size 128M;
    set $fcgi_server 127.0.0.1:9000;

    listen 80; ## listen for ipv4

    server_name api.denghp.com;
    root        /home/denghp/webroot/yii2-advanced-api/web;
    index       index.php;

    access_log  /usr/local/nginx/logs/yii2_demo_api_access.log main;
    error_log   /usr/local/nginx/logs/yii2_demo_api_error.log;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }                                                                                                                                          

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass $fcgi_server;
    }   

    location ~ /\.(ht|svn|git) {
        deny all;
    }   
}

http://api.denghp.com/v1/countries not working
http://api.denghp.com/v1/country not working

Not Found (#404)

Page not found.
The above error occurred while the Web server was processing your request.

Please contact us if you think this is a server error. Thank you.

error message

2015-06-09 21:42:12 [127.0.0.1][-][-][error][yii\web\HttpException:404] exception 'yii\web\NotFoundHttpException' with message 'Page not found.' in /home/denghp/webroot/yii2-advanced-api/vendor/yiisoft/yii2/web/Request.php:187
Stack trace:
#0 /home/denghp/webroot/yii2-advanced-api/vendor/yiisoft/yii2/web/Application.php(75): yii\web\Request->resolve()
#1 /home/denghp/webroot/yii2-advanced-api/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#2 /home/denghp/webroot/yii2-advanced-api/api/web/index.php(17): yii\base\Application->run()
#3 {main}
2015-06-09 21:42:12 [127.0.0.1][-][-][info][application] $_SERVER = [
    'USER' => 'denghp'
    'HOME' => '/home/denghp'
    'FCGI_ROLE' => 'RESPONDER'
    'QUERY_STRING' => ''
    'REQUEST_METHOD' => 'GET'
    'CONTENT_TYPE' => 'text/plain; charset=utf-8'
    'CONTENT_LENGTH' => ''
    'SCRIPT_NAME' => '/index.php'
    'REQUEST_URI' => '/v1/countris'
    'DOCUMENT_URI' => '/index.php'
    'DOCUMENT_ROOT' => '/home/denghp/webroot/yii2-advanced-api/api/web'
    'SERVER_PROTOCOL' => 'HTTP/1.1'
    'GATEWAY_INTERFACE' => 'CGI/1.1'
    'SERVER_SOFTWARE' => 'nginx/1.7.8'
    'REMOTE_ADDR' => '127.0.0.1'
    'REMOTE_PORT' => '60919'
    'SERVER_ADDR' => '127.0.0.1'
    'SERVER_PORT' => '80'
    'SERVER_NAME' => 'api.denghp.com'
    'REDIRECT_STATUS' => '200'
    'SCRIPT_FILENAME' => '/home/denghp/webroot/yii2-advanced-api/api/web/index.php'
    'HTTP_HOST' => 'api.denghp.com'
    'HTTP_CONNECTION' => 'keep-alive'
    'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
    'HTTP_CONTENT_TYPE' => 'text/plain; charset=utf-8'
    'HTTP_ACCEPT' => '*/*'
    'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, sdch'
    'HTTP_ACCEPT_LANGUAGE' => 'zh-CN,zh;q=0.8,en-GB;q=0.6,en;q=0.4,en-US;q=0.2'
    'PHP_SELF' => '/index.php'
    'REQUEST_TIME_FLOAT' => 1433857332.8264
    'REQUEST_TIME' => 1433857332
]

How can remove /web from URL?

Hi @deerawan,
how can remove /web from url?

for example localhost/api/web/v1/countries become localhost/api/v1/countries.
or remove /web/v1?

please Describe how to change the form http://api.example.com/countries

filtering actions

Hi, first, thanks for sharing.

I would like to ask you if it is possible to use the filter of actions in the urlManager by using 'only' and 'except' such as the documentation.

https://github.com/yiisoft/yii2/blob/master/docs/guide/rest-routing.md

Example:

    'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => true,
        'showScriptName' => false,
        'rules' => [
            [
                'class' => 'yii\rest\UrlRule',
                'controller' => 'v1/country',
                'only' => ['get'],
                'tokens' => [
                    '{id}' => '<id:\\w+>'
                ]

            ]
        ],
    ]

Thanks

what is purpose of aliases.php?

Hi,

Nice work, I really appreciate your effort!
I have a question!
Why you use aliases.php for paths? why you not used bootstrap.php? pls explain your logic?

Thank you

By default getting API response in XML

I am successfully able to implement this, but I have an issue be default API response is in XML format. I wanted JSON response, Can you help where I can configure response to be JSON. Thanks in advance

Migration has failed

...Exception 'yii\db\Exception' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table '(...).Country' doesn't exist

Because of first letter is capitalized in your mig

keep getting 404...

Dear Deerawan...
I'm new to yii...
trying to imlement your api..
got Exception (#2) on http://192.168.1.13:8080/yii2-advanced-api/api/web/
got 404 Not Found on http://192.168.1.13:8080/yii2-advanced-api/api/web/v1/countries

what I'm doing is clone your git then composer install then php ./init then ./yii migrate
if there any clue please info..

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.