仅查看具有Lumen POST API调用的应用程序版本

时间:2017-08-25 08:35:15

标签: routes lumen lumen-5.2 lumen-5.3 lumen-routing

我是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 (5.4.6) (Laravel Components 5.4.*)

现在我已经为简单的短信确认了一个API终点。用户应该发送和发送电话号码,我只是根据数据库中的数据验证它们并返回结果。

我的 routes.php 读取:

$app->post('/outbound/sms', 'App\Http\Controllers\SmsController@sendSms');

我的 SmsController 也出现在App \ Http \ Controllers下。它使用如下的PhoneNumber模型:

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附加到网址也不起作用。 无法弄清楚什么是错的?

更新 最后,我得到了很好的路线。我的routes.php内容应该在routes / web.php而不是App \ Http。

Nut现在我得到了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)

1 个答案:

答案 0 :(得分:0)

终于得到了NotFoundHttpException的原因:

在index.php中添加了以下行:

$app = require __DIR__.'/../bootstrap/app.php';

$request = Illuminate\Http\Request::capture();

$app->run($request);

根据以下链接解决方案:

http://laravel-tricks.com/tricks/notfoundhttpexception-lumen

对于偶然发现此问题的任何人,请务必将您的路线保留在 /routes/web.php