deerawan / yii2-advanced-api

Yii2 advanced template with RESTful API setup
Other
193 stars 157 forks source link

How can remove /web from URL? #7

Open faravaghi opened 8 years ago

faravaghi commented 8 years ago

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

deerawan commented 8 years ago

Hi @faravaghi

It is better if you use virtual host so you can decide what URL you want to use. More info https://httpd.apache.org/docs/2.2/vhosts/examples.html

faravaghi commented 8 years ago

thx friend, and remove /web/v1 with htaccess rule?

deerawan commented 8 years ago

@faravaghi yes, it can :)

faravaghi commented 8 years ago

well, how?

mvofreire commented 8 years ago

@faravaghi You found any solution?

faravaghi commented 8 years ago

Hi mvofreire, No can not, but in another way I solved in cpanel create a subdomain and link to /public_html/api/web and now i can access this link http://api.example.com/v1/countries

mvofreire commented 8 years ago

@faravaghi i found a simple solution, but i dont know is better one!

i created a file "index.php" at /api - (root) with the following code

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/../common/config/bootstrap.php');

$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../common/config/main.php'),
    require(__DIR__ . '/../common/config/main-local.php'),
    require(__DIR__ . '/config/main.php')
);

$application = new yii\web\Application($config);
$application->run();

And i Created .htaccess file in the same directory

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

I tested in postMan "http://localhost/serverRest/api/v1/countries" and got this response

[{"code":"AU","name":"Australia","population":18886000},{"code":"BR","name":"Brazil","population":170115000},{"code":"CA","name":"Canada","population":1147000},{"code":"CN","name":"China","population":1277558000},{"code":"DE","name":"Germany","population":82164700},{"code":"FR","name":"France","population":59225700},{"code":"GB","name":"United Kingdom","population":59623400},{"code":"IN","name":"India","population":1013662000},{"code":"RU","name":"Russia","population":146934000},{"code":"US","name":"United States","population":278357000}]

-- Sorry for my English

futuralis commented 8 years ago

ISSUE1: Invalid Parameter – yii\base\InvalidParamException The file or directory to be published does not exist: ..../yii2-api/vendor/bower/jquery/dist

I see a vendor/bower-asset directory, but not a vendor/bower directory....

Please help.

SOLVED: by renaming vendor/bower-asset directory to vendor/bower

ISSUE2: yii\web\Request::cookieValidationKey must be configured with a secret key.

SOLVED: under common/config/main-local.php add

return [ 'components' => [ .................... 'request' => [ // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation 'cookieValidationKey' => 'uRqj3HcbYA5MFxF3', ], ], ];