启发我-获取此代码流明的功能

问题描述 投票:0回答:1

我正在学习流明,从未与它或其大兄弟laravel合作。我曾经编写“ vanilla” PhP大约1 1/2年,例如,我熟悉PDO请求的功能。

所以我正在使用本教程:https://www.youtube.com/watch?v=6Oxfb_HNY0U

创建完我的数据库后,到目前为止它只有6个列的“ articles”表,我从教程中尝试了以下代码:

web.php(位于“ routes”文件夹中:]]

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$router->get('/', function () use ($router) {
    return $router->app->version();
});


$router->group(['prefix' => 'api'], function($router){
  $router->get('articles', 'ArticleController@showAllArticles');
});


$router->get('foo', function () {
    return 'Hello World';
});

$router->post('foo', function () {
    //
});

app.php(位于“引导程序”中:):

<?php

require_once __DIR__.'/../vendor/autoload.php';

(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
    dirname(__DIR__)
))->bootstrap();

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new Laravel\Lumen\Application(
    dirname(__DIR__)
);

// $app->withFacades();
 $app->withEloquent();

/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/

// $app->middleware([
//     App\Http\Middleware\ExampleMiddleware::class
// ]);

// $app->routeMiddleware([
//     'auth' => App\Http\Middleware\Authenticate::class,
// ]);

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

// $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

$app->router->group([
    'namespace' => 'App\Http\Controllers',
], function ($router) {
    require __DIR__.'/../routes/web.php';
});

return $app;

Article.php(位于“ app”文件夹中:]]

<?php

namespace App;


use Illuminate\Database\Eloquent\Model;

class Article extends Model
{

    protected $fillable = [
        'title', 'description','status'
    ];


}

ArticleController.php(位于\ Http \ Controllers内部)

<?php

namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Requests;
class ArticleController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    //

    public function showAllArticles(){
      return response()->json(Article::all());
    }
}

现在,令人困惑的是此语法的工作方式:

return response()->json(Article::all());

来自ArticleController.php。

据我所知,对此函数的调用已在web.php中定义为:

$router->group(['prefix' => 'api'], function($router){
  $router->get('articles', 'ArticleController@showAllArticles');
});

这里定义了要访问的表,然后还定义了用于处理来自数据库的响应的函数。我认为到目前为止,我还算不错。

但是当我现在尝试将此框架语法“翻译”成其PHP相关信息时,我感到困惑。是什么:

Article::all()

内部

return response()->json(Article::all());

做?什么是文章?我猜这是表文章中的一行。这里的命名是任意的,对吗?然后是“ all()”。我想到的第一个猜测是PDO等效的“ fetchAll()”。真的吗?如果在基于PDO的数据库查询中使用了fetchAll(),行为是否相同?语法本身是一种直观的方法,但是仍然为不同的解释留有余地。由于我认为Article是响应中的单行,因此是“管道”到“ all()”函数的,因此all()可以做的事情与总是应用到“ all()”的“ fetchAll()”不同查询的全部结果,而不仅仅是单个结果集(=行)。

此外,有人知道Lumen的好教程吗?仅与官方文档一起工作真是太可怕了,因为该框架是如此模块化,仅阅读不同部分并没有帮助我建立一个小的测试项目,从中我可以学习如何实际使用该框架,而不仅仅是描述它...

我正在学习流明,从未与它或其大兄弟laravel合作。我曾经编写过“ vanilla” PhP大约1 1/2年,例如,我熟悉PDO请求的功能。所以我是...

laravel lumen
1个回答
1
投票

Laravel和Lumen都使用口才模型,这就是您的ArticleEloquent model.

模型允许您查询表中的数据,以及在表中插入新记录。

© www.soinside.com 2019 - 2024. All rights reserved.