Laravel 如何从子域 URL 中删除“api”前缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49598913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel How to remove "api" Prefix from subdomain URL
提问by Lakshmaji
I have created a Laravel application which is both Web application and provides REST APIs to android and iOS platforms.
我创建了一个 Laravel 应用程序,它既是 Web 应用程序,又为 android 和 iOS 平台提供 REST API。
I have two route files one is api.php and other is web.php and routes\api.php routing as follows:
我有两个路由文件,一个是 api.php,另一个是 web.php 和 routes\api.php 路由如下:
routes/api.php
Route::group([
'domain'=>'api.example.com',
function(){
// Some routes ....
}
);
and nginx serve blocks configured can be seen here
和配置的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;
}
}
}
I was be able to access my application using http://example.com
for web application and http://api.example.com/api/cities
for REST API's. But the subdomain URL contains apias prefix as given below.
我能够使用http://example.com
Web 应用程序和http://api.example.com/api/cities
REST API访问我的应用程序 。但是子域 URL 包含api作为前缀,如下所示。
http://api.example.com/api/cities
But i want to my subdomain like this http://api.example.com/cities
(I wanted to remove
api prefix from the sub domain URL).
但我想要这样的子域http://api.example.com/cities
(我想remove
从子域 URL获取api 前缀)。
Is it right way to remove prefix apiin RouteServiceProvide.php
for api routes?
在api 路由中删除前缀api是正确的方法RouteServiceProvide.php
吗?
Or is they any right way to implement this?
或者他们有什么正确的方法来实现这个吗?
Environment Details Laravel 5.5 (LTS) PHP 7.0
环境细节 Laravel 5.5 (LTS) PHP 7.0
回答by Samir Mammadhasanov
It's just prefix to differ your api routes from other routes. You can add something different from api
to here.
它只是将您的 api 路由与其他路由区分开来的前缀。您可以添加api
与此处不同的内容。
In app\Providers\RouteServiceProvider
change this function:
在app\Providers\RouteServiceProvider
改变这个功能:
/**
* 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'));
}
Remove prefixe line:
删除前缀行:
/**
* 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'));
}