我创建了一个Laravel应用程序,它既是Web应用程序又为Android和iOS平台提供REST API。
我有两个路由文件,一个是api.php,另一个是web.php和routes \ api.php路由,如下所示:
routes/api.php
Route::group([
'domain'=>'api.example.com',
function(){
// Some routes ....
}
);
配置的和nginx服务块可以在这里看到
server {
listen 80;
listen [::]:80;
root /var/www/laravel/public;
index index.php;
server_name api.example.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
我可以使用http://example.com
访问我的应用程序以获取Web应用程序,使用http://api.example.com/api/cities
访问REST API。但子域名网址包含 api 作为前缀,如下所示。
http://api.example.com/api/cities
但是我希望像我这样的子域名http://api.example.com/cities
(我想从子域网址中获取remove
api前缀。)
在api路由的RouteServiceProvide.php
中删除前缀 api 是否正确?
或者他们是否有正确的方法来实现这个目标?
环境详情 Laravel 5.5(LTS) PHP 7.0
答案 0 :(得分:10)
这只是将api路线与其他路线区分开来的前缀。您可以在此处添加与api
不同的内容。
在app\Providers\RouterServiceProvider
中更改此功能:
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
删除前缀行:
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
答案 1 :(得分:0)
实际上在Laravel 8中,我只是从App / Providers / RouteServiceProvider.php中的前缀中删除api
def superhero_name_generator():
print("Welcome to the superhero name generator:\nTo choose your name follow the instructions below")
first_part = input("Please enter the name of the city you grew up in. ")
second_part = input("Please enter the name of your pet or your favorite mythical creature (Ie. Dragon). ")
superhero_name = first_part + second_part
return superhero_name
end = 1
name = []
while end > 0 :
var = superhero_name_generator()
name.append(var)
print("your superhero name is {}".format(var))
print("If you want to generate another name please press 1 to quit press 0")
end = int(input())
else:
print("Go out and save the world",name)
到
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));