Lumen - mongodb - jenssegers/laravel-mongodb - 邮递员

2023-12-06

我已经在我的wamp上安装了mongodb,C:\wamp64\bin\mongodb\mongodb.3.4\bin,我在路径中添加了 mongodb,并创建 Windows 服务以在必要时启动它。 我已经通过 Composer 安装了 lumen,之后我安装了:

  • "laravel/lumen-framework": "5.3.*",
  • “barryvdh/laravel-ide-helper”:“v2.2.1”,
  • jenssegers/laravel-mongodb:“v3.1.3”
  • “jenssegers/mongodb-session”:“v1.1.0”

终于我安装了mongodb.dll在我的 wamp php 上并在 php.ini 中添加 extension=php_mongodb.dll 。 现在 mongodb 上的扩展已激活。

This is my User class: enter image description here

这是我的迁移

    <?php

    use Illuminate\Support\Facades\Schema;
    use Jenssegers\Mongodb\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreateUsersTable extends Migration
    {
        /**
         * The name of the database connection to use.
         *
         * @var string
         */
        protected $connection = 'mongodb';

        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::connection($this->connection)->
            table('Users', function (Blueprint $collection) {
                $collection->index('id');
                $collection->string('name');
                $collection->string('surname');
                $collection->unique('username');
                $collection->string('password',64);
                $collection->timestamps();
            });
        }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::connection($this->connection)
            ->table('Users', function (Blueprint $collection)
            {
                $collection->drop();
            });
    }
}`

这是 .env 文件

APP_ENV=local
APP_DEBUG=true
APP_KEY=
APP_TIMEZONE=UTC

DB_CONNECTION=mongodb
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

MONGODB_HOST=localhost
MONGODB_PORT=27017
MONGODB_USERNAME=joy
MONGODB_PASSWORD=mongo@ad@joy
MONGODB_DATABASE=brasserie
MONGODB_AUTHDATABASE=admin

CACHE_DRIVER=file
SESSION_DRIVER=file

我在根应用程序中创建了配置目录,并且添加了一个database.php配置文件:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | PDO Fetch Style
    |--------------------------------------------------------------------------
    |
    | By default, database results will be returned as instances of the PHP
    | stdClass object; however, you may desire to retrieve records in an
    | array format for simplicity. Here you can tweak the fetch style.
    |
    */

    'fetch' => PDO::FETCH_CLASS,

    /*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'mongodb'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */

    'connections' => [



        'mongodb' => array(
            'driver'   => 'mongodb',
            'host'     => env('MONGODB_HOST', '127.0.0.1'),
            'port'     => env('MONGODB_PORT', 27017),
            'username' => env('MONGODB_USERNAME', 'root'),
            'password' => env('MONGODB_PASSWORD', 'testbrasserie'),
            'database' => env('MONGODB_DATABASE', 'synthese'),
           'options' => array(
                'db' => env('MONGODB_AUTHDATABASE', 'admin') //Sets the auth DB
            )//*/
        ),

    ],

    /*
    |--------------------------------------------------------------------------
    | Migration Repository Table
    |--------------------------------------------------------------------------
    |
    | This table keeps track of all the migrations that have already run for
    | your application. Using this information, we can determine which of
    | the migrations on disk haven't actually been run in the database.
    |
    */

    'migrations' => 'migrations',

    /*
    |--------------------------------------------------------------------------
    | Redis Databases
    |--------------------------------------------------------------------------
    |
    | Redis is an open source, fast, and advanced key-value store that also
    | provides a richer set of commands than a typical key-value systems
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'cluster' => env('REDIS_CLUSTER', false),

        'default' => [
            'host'     => env('REDIS_HOST', '127.0.0.1'),
            'port'     => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DATABASE', 0),
            'password' => env('REDIS_PASSWORD', null),
        ],

    ],//*/

];

我已经启用了 eloquent、facades 和 Messenger 服务提供者。这是我的 bootstrap/app.php:

<?php

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

try {
    (new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    //
}

/*
|--------------------------------------------------------------------------
| 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(
    realpath(__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);


if ($app->environment() !== 'production') {
    $app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}

//class_alias ('Jenssegers\Mongodb\Eloquent\Model', 'Moloquent');
$app->register('Jenssegers\Mongodb\MongodbServiceProvider');

$app->withEloquent();
/*
|--------------------------------------------------------------------------
| 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->group(['namespace' => 'App\Http\Controllers'], function ($app) {
    require __DIR__.'/../routes/web.php';
});

$app->configure('database');

return $app;

Here is my UsersTableSeeder: enter image description here

I have launch the artisan migrate:install then artisan migrate and finally artisan db:seed and it seems to be ok, here is the result inside mongo: enter image description here

然后我创建了一个 UserController

<?php
/**
 * Created by PhpStorm.
 * User: Joy_Admin
 * Date: 09/12/2016
 * Time: 23:23
 */

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use DB;


class UserController extends Controller
{


    /**
     * Pour recupérer tous les utilsateurs de la BD
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $users = User::all();
        return response()->json($users);
    }

    /**
     * Pour recupérer tous les utilsateurs de la BD
     * @return \Illuminate\Http\JsonResponse
     */
    public function test()
    {

        return response()->json("it's ok");
    }

    /**
     * pour enregistrer un nouvel utilisateur dans la base de données
     * @param Request $request
     */
    public function create(Request $request)
    {

        $user = new User();

       $user->name = $request->input('name');
      $user->surname = $request->input('surname');
       $user->username = $request->input('username');
       $user->password = Hash::make($request->input('password'));
        $user->save();//*/


        DB::collection('Users')->insert([
            'name'     => 'name1',
            'surname' => 'surname1',
            'username'    => 'username1',
            'password' => Hash::make('password1')
        ]);
        return response()->json($user);

    }


    /**
     * On renvoit l'individu dans la BD
     * correspondant à l'id spécifié
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function get($id)
    {
        $user = User::find($id);

        return response()->json($user);
    }

    /**
     * Mettre à jour les informations sur un utilisateur de la BD
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request,$id)
    {
        $user = User::find($id);

        $user->name = $request->input('name');
        $user->surname = $request->input('surname');
        $user->username = $request->input('username');
        $user->password = Hash::make($request->input('password'));

        $user->save();

        return response()->json($user);
    }

    public function delete($id)
    {
        $user = User::find($id);

        $user->delete();

        return response()->json('Success');

    }
}

最后更新我的routes/web.php

<?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.
|
*/

use App\Http\Controllers\UserController;



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

$app->get('/api/users','UserController@index');
$app->get('/api/users/{id}','UserController@get');
$app->post('/api/users','UserController@create');
$app->put('/api/users/{id}','UserController@update');
$app->delete('/api/users/{id}','UserController@delete');
$app->get('/api','UserController@test');

我启动了邮递员,以便我可以测试我的应用程序, 仅有的/api工作,所有其他路线都给我同样的错误

  1. for $app->get('/api','UserController@test');
  2. for $app->get('/api/users','UserController@index');
  3. for $app->get('/api/users/{id}','UserController@get');
  4. for $app->delete('/api/users/{id}','UserController@delete');

有人可以帮我解决这个问题吗?

Openssl 和curl 在我的wamp php 中处于活动状态。


我已经解决问题了 在我的 .env 中,我有:

CACHE_DRIVER=file
SESSION_DRIVER=file

但在我的项目中没有对它们进行配置,所以当我将其更改为

CACHE_DRIVER=
SESSION_DRIVER=

现在一切正常。

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Lumen - mongodb - jenssegers/laravel-mongodb - 邮递员 的相关文章

随机推荐

  • 如何在还使用 keycloak 提供网页的客户端上使用计划任务?

    我在用着Spring Boot and Keycloak开发一个网络应用程序 然后我写了一个计划任务 我正在使用KeycloakRestTemplate向另一个应用程序询问一些数据 如下所示 Override Scheduled cron
  • 增加 Java.sql.date/时间的最佳方法是什么?

    我有一个问题 我有一个 MySQL 数据库将日期和时间存储在单独的列中 但是 在 Java 代码中 我需要将数据库中日期和时间的结果时间戳增加分钟 小时或天 然后更新数据库中的相应列 我目前在 Java 中使用 Java sql date
  • 将旧内容(.html/.php 等)重新路由到 Ruby on Rails

    我已切换到 Ruby on Rails 当前的问题是重新路由 旧内容如XXX dummy html or XXX dummy php in RoR 到底什么是最好的解决方案 孤立的内容 XXX onlyinstance html 具有如下内
  • Visual Studio 代码。为什么窗口控制按钮位于左侧菜单栏上方?

    我正在开发视觉工作室应用程序 突然 我的窗口控制按钮跳到主菜单导航栏的左侧 隐藏按钮 文件 和 编辑 有谁知道如何将 Windows 控制按钮重新放回导航栏的右侧 在 vscode v1 71 2 中window experimental
  • 在 Kivy 画布上显示 PIL 图像

    我找不到任何有关如何在 Kivy Canvas 上显示 PIL 图像的文档 Rectangle source image give TypeError Image object has no attribute getitem 由于其他图像
  • 在Windows应用程序中调用Web服务

    我有一个 Windows 应用程序 我要求用户通过网站中提供给用户的用户名和密码登录应用程序 为此 我首先检查用户是否连接到互联网 如果存在连接 则继续检查凭据是否有效 因此 我需要在Windows应用程序中调用Web服务 我该如何进行 欢
  • 使用 firebase.com 作为数据库的应用程序中的分页

    前端应用程序将使用 firebase com 作为数据库 应用程序应该像博客一样工作 管理员添加 删除帖子 有 主页 页面 每页显示多篇文章 带有 下一页 和 上一页 按钮 以及显示单篇文章的页面 我还需要深层链接 IE 当用户输入 URL
  • Backbone.Collection.Create 未在视图中触发“添加”

    希望这是一个简单的问题 我正在努力学习骨干 但我坚持做一件非常简单的事情 当我使用 create 方法更新集合时 视图上的渲染永远不会被调用 我认为这应该在不显式调用 render 的情况下发生 我没有加载任何动态的东西 在这个脚本触发之前
  • 在android中启动2个模拟器

    我想知道是否有任何简单的方法可以在 android 中启动 2 个模拟器 我已经查看了开发人员指南 但没有足够的信息来帮助我 请有人解释一下这是如何完成的 谢谢 据我所知 您不能两次启动同一个模拟器 但通过创建两个单独的模拟器 您可以同时启
  • 休息最佳实践:何时返回 404 not found

    如果我有以下休息电话 GET items id subitems 在这些情况下我们应该返回以下内容吗 If id 没有找到 我们应该返回吗404 Not Found If id 已找到但未找到子项 我们是否应该返回200 Ok和一个空数组
  • 更新约会时时区更改为 UTC

    我正在使用 EWS 1 2 发送预约 创建新约会时 时区在通知邮件上正确显示 但在更新同一约会时 时区会重置为 UTC 有人能帮我解决这个问题吗 以下是复制该问题的示例代码 ExchangeService service new Excha
  • 使用mysql、php和ajax(使用jquery)创建表

    对于我的新项目 我想要一种不需要在每个数据库请求上重新加载页面的现代方法 我希望脚本查询数据库并使用查询信息创建一个表 我尝试了在互联网上找到的不同脚本 下面这个最接近我的需求 索引 php
  • 重定向回来时 LightOpenID 被禁止

    我正在尝试使用 lightOpenID 它应该很简单 并且上传文件然后测试它是否有效 当我使用 example google php 时 我第一次点击登录按钮 它要求我登录 Google 并允许 记住我正在构建的网站 然后它重定向回 exa
  • 在C语言中,数组是指针还是用作指针?

    我的理解是 数组只是指向值序列的常量指针 当您在 C 中声明数组时 您就是在声明一个指针并为其指向的序列分配空间 但这让我很困惑 以下代码 char y 20 char z y printf y size is lu n sizeof y
  • 需要将ascii值转换为hex值

    我需要将 ascii 值转换为十六进制值 请参阅 Ascii 表 但我在下面列出了一些示例 ASCII 1 31 2 32 3 33 4 34 5 35 A 41 a 61 等 但我使用 int 而不是字符串值 是否可以这样做 因此int测
  • Jekyll 多页分页

    我是 html css 新手 但正在尝试使用 Jekyll 创建一个博客 我在这里找到了这个主题https github com rosario kasper 主页index html 包含分页列表中的所有帖子 这很酷 不过 我想将我的帖子
  • Grails 条件查询检查“OR”逻辑

    我有圣杯criteriaQuery我正在检查的地方OR针对单个状态变量的逻辑如下 or eq status Status ONE eq status Status TWO eq status Status THREE 这段代码工作正常 我的
  • 从 firestore flutter 中的子集合中获取数据

    在第一个屏幕截图中 收集了许多文档users 每个文档都包含进一步的集合jobPost该集合包含更多文档及其元数据 我在这里想要的是转到集合的每个文档users和进一步的子集合jobPost并获取所有文档 假设首先应该转到集合中的文档 1u
  • 将 jQuery 日期选择器应用于多个实例

    我有一个 jQuery 日期选择器控件 可以在一次实例中正常工作 但我不确定如何让它在多个实例中工作 br 如果没有 For Each 循环 它可以正常工作 但如果 MyRecords 集合中有多个项目 则只有第一个文本框获得日期选择器 这
  • Lumen - mongodb - jenssegers/laravel-mongodb - 邮递员

    我已经在我的wamp上安装了mongodb C wamp64 bin mongodb mongodb 3 4 bin 我在路径中添加了 mongodb 并创建 Windows 服务以在必要时启动它 我已经通过 Composer 安装了 lu