[英]Seeing only the app version with Lumen POST API call
I am new to Lumen.我是 Lumen 的新手。 I started using Lumen on MAC OS and running it inside a docker container.
我开始在 MAC OS 上使用 Lumen,并在 docker 容器中运行它。
The app service in the dockerfile is as follows: dockerfile中的app服务如下:
app:
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
The mysql service is running perfectly as well. mysql 服务也运行良好。 On opening http://localhost:9002 I can see the normal Lumen app versioning:
打开http://localhost:9002我可以看到正常的 Lumen 应用程序版本控制:
Lumen (5.4.6) (Laravel Components 5.4.*)
Now I have made an API end-point for simple sms to be acknowledged.现在我已经为要确认的简单短信创建了一个 API 端点。 The user should send from and to phone numbers and I just validate them against the data present in the db and return the result.
用户应该发送和发送电话号码,我只是根据数据库中存在的数据验证它们并返回结果。
My routes.php reads:我的routes.php内容如下:
$app->post('/outbound/sms', 'App\Http\Controllers\SmsController@sendSms');
My SmsController is present under App\Http\Controllers as well.我的SmsController也位于 App\Http\Controllers 下。 It uses a PhoneNumber Model as below:
它使用电话号码 Model,如下所示:
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'=> '']);
}
My .htaccess file reads:我的.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>
On making a POST API call from Postman,从 Postman 拨打 POST API 时,
http://localhost:9002/outbound/sms?from=1234567890&to=3456789&text=hi
I always get the app version as the response:我总是得到应用程序版本作为响应:
Lumen (5.4.6) (Laravel Components 5.4.*)
Even appending index.php to the url doesn't work.即使将 index.php 附加到 url 也不起作用。 Can't make out what's wrong?
看不出哪里出了问题?
Update: Finally, I got routes working fine.更新:最后,我的路线运行良好。 My routes.php content should have been in routes/web.php instead of App\Http.
我的 routes.php 内容应该在 routes/web.php 而不是 App\Http。
Nut now I am getting NotFoundHttpException.坚果现在我得到 NotFoundHttpException。 Below is the stack trace::
下面是堆栈跟踪::
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)
Finally got the reason for that NotFoundHttpException: 终于有了那个NotFoundHttpException的原因:
Added the below lines in index.php: 在index.php中添加了以下几行:
$app = require __DIR__.'/../bootstrap/app.php';
$request = Illuminate\Http\Request::capture();
$app->run($request);
Solution as per the below link: 根据以下链接的解决方案:
http://laravel-tricks.com/tricks/notfoundhttpexception-lumen . http://laravel-tricks.com/tricks/notfoundhttpexception-lumen 。
For anyone who stumbles upon this question , please make sure to keep your routes in /routes/web.php . 对于任何偶然发现此问题的人,请确保将您的路由保存在/routes/web.php中 。
I came across same situation, in order to use the Lumen for REST API, Moving the routes from App/routes.php to routes/web.php worked for me.我遇到了同样的情况,为了将 Lumen 用于 REST API,将路由从 App/routes.php 移动到 routes/web.php 对我有用。 It did not require me to change the index.php.
它不需要我更改 index.php。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.