Php Lumen-mongodb-jenssegers/laravel mongodb-邮递员
我已经在我的wamp上安装了mongodb,C:\wamp64\bin\mongodb\mongodb.3.4\bin,我已经在路径中添加了mongodb,并创建了windows服务,以便在必要时启动它。 我通过composer安装了lumen,之后我安装了:Php Lumen-mongodb-jenssegers/laravel mongodb-邮递员,php,mongodb,api,postman,lumen,Php,Mongodb,Api,Postman,Lumen,我已经在我的wamp上安装了mongodb,C:\wamp64\bin\mongodb\mongodb.3.4\bin,我已经在路径中添加了mongodb,并创建了windows服务,以便在必要时启动它。 我通过composer安装了lumen,之后我安装了: “laravel/lumen框架”:“5.3.*”, “barryvdh/laravel ide帮助程序”:“v2.2.1”, jenssegers/laravel mongodb:“v3.1.3” “Jenssers/mongodb会
- “laravel/lumen框架”:“5.3.*”,
- “barryvdh/laravel ide帮助程序”:“v2.2.1”,
- jenssegers/laravel mongodb:“v3.1.3”
- “Jenssers/mongodb会话”:“v1.1.0”
<?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();
});
}
}`
我在根应用程序中创建了配置目录,并添加了一个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),
],
],//*/
];
我已经解决了这个问题,
在我的.env中,我有:
CACHE_DRIVER=file
SESSION_DRIVER=file
但在我的项目中没有为它们配置,所以当我将其更改为
CACHE_DRIVER=
SESSION_DRIVER=
现在一切都好了
<?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');
}
}
<?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');
CACHE_DRIVER=file
SESSION_DRIVER=file
CACHE_DRIVER=
SESSION_DRIVER=