Hi CeBe:
I redo from start with myyii2.app
Blockquote
$ composer create-project --prefer-dist yiisoft/yii2-app-basic app
There were a lot of “suggestions” such as
sebastian/global-state suggests installing ext-uopz (*)
but I did not do anything.
$ sudo chmod -R 777 app
In mysql
CREATE TABLE user
(
id
int(11) NOT NULL AUTO_INCREMENT,
username
varchar(255) NOT NULL,
auth_key
varchar(32) NOT NULL,
password_hash
varchar(255) NOT NULL,
email
varchar(100) NOT NULL,
status
smallint(10) NOT NULL,
created_at
int(11) NOT NULL,
updated_at
int(11) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE country
(
code
char(2) NOT NULL,
name
char(52) NOT NULL,
population
int(11) NOT NULL DEFAULT ‘0’,
PRIMARY KEY (code
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Add some data into the table country.
Change app/config/db.php
Go to browser
myyii2.api/index.php?r=gii
generate models
app/models/User.php
app/models/Country.php
add
app/controls/UserController.php
app/controls/CountryController.php
change app/config/web.php
Blockquote
This is the results with curl
Blockquote
andy@andy-Inspiron-15-3567:~/websites$ curl -i -X OPTIONS “http://myyii2.app/countries”
HTTP/1.1 200 OK
Date: Sat, 10 Nov 2018 22:23:57 GMT
Server: Apache/2.4.29 (Ubuntu)
Allow: GET, POST, HEAD, OPTIONS
Access-Control-Allow-Methods: GET, POST, HEAD, OPTIONS
X-Debug-Tag: 5be75a7dd4088
X-Debug-Duration: 6
X-Debug-Link: /debug/default/view?tag=5be75a7dd4088
Content-Length: 0
Content-Type: application/json; charset=UTF-8
andy@andy-Inspiron-15-3567:~/websites$ curl -i “http://myyii2.app/countries”
HTTP/1.1 200 OK
Date: Sat, 10 Nov 2018 22:24:52 GMT
Server: Apache/2.4.29 (Ubuntu)
X-Pagination-Total-Count: 16
X-Pagination-Page-Count: 1
X-Pagination-Current-Page: 1
X-Pagination-Per-Page: 20
Link: http://myyii2.app/countries?page=1; rel=self
X-Debug-Tag: 5be75ab4c3e8c
X-Debug-Duration: 7
X-Debug-Link: /debug/default/view?tag=5be75ab4c3e8c
Content-Length: 837
Content-Type: application/json; charset=UTF-8
[{“code”:“A1”,“name”:“Aaaaaaa1”,“population”:1000},{“code”:“A2”,“name”:“A2”,“population”:2000},{“code”:“AU”,“name”:“Australia”,“population”:24016400},{“code”:“B1”,“name”:“B1”,“population”:1000000},{“code”:“B2”,“name”:“B2”,“population”:2000000},{“code”:“BR”,“name”:“Brazil”,“population”:205722000},{“code”:“CA”,“name”:“Canada”,“population”:35985751},{“code”:“CN”,“name”:“China”,“population”:1375210000},{“code”:“DE”,“name”:“Germany”,“population”:81459000},{“code”:“FR”,“name”:“France”,“population”:64513242},{“code”:“GB”,“name”:“United Kingdom”,“population”:65097000},{“code”:“IN”,“name”:“India”,“population”:1285400000},{“code”:“LA”,“name”:“Laos”,“population”:20000000},{“code”:“RU”,“name”:“Russia”,“population”:146519759},{“code”:“US”,“name”:“United States”,“population”:322976000},{“code”:“VN”,“name”:“Vietnam”,“population”:100000000}]
andy@andy-Inspiron-15-3567:~/websites$ curl -i -H “Accept:application/json” -H “Content-Type:application/json” -XPOST “http://myyii2.app/countries” -d ‘{“code” : “B3”, “name” : “B3”, “population” : “3000000”}’
HTTP/1.1 201 Created
Date: Sat, 10 Nov 2018 22:25:44 GMT
Server: Apache/2.4.29 (Ubuntu)
Location: http://myyii2.app/country/view?id=B3
X-Debug-Tag: 5be75ae8d814d
X-Debug-Duration: 50
X-Debug-Link: /debug/default/view?tag=5be75ae8d814d
Content-Length: 48
Content-Type: application/json; charset=UTF-8
{“code”:“B3”,“name”:“B3”,“population”:“3000000”}
andy@andy-Inspiron-15-3567:~/websites$ curl -i “http://myyii2.app/countries/A1”
HTTP/1.1 404 Not Found
Date: Sat, 10 Nov 2018 22:26:23 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Length: 286
Content-Type: text/html; charset=iso-8859-1
404 Not Found
Not Found
The requested URL /countries/A1 was not found on this server.
Apache/2.4.29 (Ubuntu) Server at myyii2.app Port 80
Blockquote
This is the apache2 config file
myyii2.app.conf
Blockquote
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request’s Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin ngothean1@gmail.com
ServerName myyii2.app
ServerAlias www.myyii2.app
DocumentRoot /home/andy/websites/app/web
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/andy/websites/app/web/>
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
# if $showScriptName is false in UrlManager, do not allow accessing URLs with script name
RewriteRule ^index.php/ - [L,R=404]
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all granted
</Directory>
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Blockquote
Thank you very much