我是 Lumen 的新手。我开始在 MAC OS 上使用 Lumen 并在 docker 容器中运行它。
dockerfile中的app服务如下:
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
mysql 服务也运行良好。打开http://localhost:9002我可以看到正常的 Lumen 应用程序版本控制:
Lumen (5.4.6) (Laravel Components 5.4.*)
现在我已经为简单的短信创建了一个 API 端点来确认。用户应该发送和发送电话号码,我只是根据数据库中存在的数据验证它们并返回结果。
我的routes.php内容如下:
$app->post('/outbound/sms', 'App\Http\Controllers\SmsController@sendSms');
我的SmsController也存在于 App\Http\Controllers 下。它使用如下的电话号码模型:
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\PhoneNumber;
use Illuminate\Support\Facades\App;
public function sendSms(Request $request)
{
try
{
$fromModel = PhoneNumber::findOrFail($request->input('from'));
}
catch(ModelNotFoundException $e)
{
return response()->json(['message'=> '', 'error'=> 'from is not found']);
}
try
{
$toModel = PhoneNumber::findOrFail($request->input('to'));
}
catch(ModelNotFoundException $e)
{
return response()->json(['message'=> '', 'error'=> 'to is not found']);
}
$this->validateSms($_REQUEST);
return response()->json(['message'=> 'inbound sms ok', 'error'=> '']);
}
我的.htaccess文件内容如下:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
从 Postman 发出 POST API 调用时,
http://localhost:9002/outbound/sms?from=1234567890&to=3456789&text=hi
我总是得到应用程序版本作为响应:
Lumen (5.4.6) (Laravel Components 5.4.*)
即使将 index.php 附加到 url 也不起作用。看不出有什么问题?
更新: 最后,我的路线运行良好。我的 routes.php 内容应该在 routes/web.php 而不是 App\Http。
坚果现在我得到了 NotFoundHttpException。下面是堆栈跟踪::
in RoutesRequests.php (line 594)
at Application->handleDispatcherResponse(array(0))
in RoutesRequests.php (line 532)
at Application->Laravel\Lumen\Concerns\{closure}()
in RoutesRequests.php (line 781)
at Application->sendThroughPipeline(array(), object(Closure))
in RoutesRequests.php (line 534)
at Application->dispatch(object(Request))
in RoutesRequests.php (line 475)
at Application->run(object(Request))
in index.php (line 29)