1. 程式人生 > >Slim 和 laravel ORM運用

Slim 和 laravel ORM運用

{
    "require": {
        "slim/slim": "3.*",
        "illuminate/database":"~4.2"
    }
}

composer 載入

require __DIR__ .'/vendor/autoload.php';
require __DIR__ .'/database.php';
require __DIR__ .'/user.class.php';
require __DIR__ .'/User.php';

use \Psr\Http\Message\ServerRequestInterface as Request
; use \Psr\Http\Message\ResponseInterface as Response; $app = new \Slim\App(array('debug'=>true)); $app->get('/hello/{name}', function (Request $request, Response $response) { $name = $request->getAttribute('name'); $response->getBody()->write("Hello, $name"); return $response
; }); $app->get('/', 'get_cpam_users'); $app->get('/cpam_users/{id}', function(Request $request, $response, $args) { get_cpam_users_id($args['id']); }); $app->post('/cpam_users_add', function(Request $request, $response, $args) { $queryParams = $request->getParams(); file_put_contents('aa.txt'
,json_encode($queryParams),FILE_APPEND); add_cpam_users($queryParams); // add_cpam_users($request->getParsedBody()); }); $app->put('/update_cpam_users', function(Request $request, $response, $args) { update_cpam_users($request->getParams()); }); $app->delete('/delete_cpam_users', function(Request $request, $response, $args) { delete_cpam_users($request->getParams()); }); $app->run();

index.php

require __DIR__ .'/vendor/autoload.php';
$database = [
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'test',
    'username'  => 'root',
    'password'  => 'root',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
];

use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;
// 建立連結
$capsule->addConnection($database);

// 設定全域性靜態可訪問
$capsule->setAsGlobal();

// 啟動Eloquent
$capsule->bootEloquent();

database.php連結資料庫



use  Illuminate\Database\Eloquent\Model  as Eloquent;

class User extends  Eloquent
{
protected $table = 'users';
}

Model User

//包含Eloquent的初始化檔案
include __DIR__ . '/database.php';

use Illuminate\Database\Capsule\Manager as Capsule;

Capsule::table('users')->insert(array(
    array('username' => 'Hello',  'email' => '[email protected]'),
    array('username' => 'Carlos',  'email' => '[email protected]'),
    array('username' => 'Overtrue',  'email' => '[email protected]'),
));

insert.php

include __DIR__ . '/database.php';

use Illuminate\Database\Capsule\Manager as Capsule;

Capsule::schema()->create('users', function($table)
{
    $table->increments('id');
    $table->string('username', 40);
    $table->string('email')->unique();
    $table->timestamps();
});

create table.php


function get_cpam_users($response) {
    $oUser = User::all();
//    try
//    {
//
//        $response = $response->withStatus(200)->withHeader('Content-type', 'application/json');
//        $response->getBody()->write(json_encode(
//                [
//                    'status' => 200,
//                    'error' => '',
//                    'datas' => $oUser
//                ]
//            )
//        );
//        return $response;
//    } catch(Exception $e) {
//        $response = $response->withStatus(500)->withHeader('Content-type', 'application/json');
//        $response->getBody()->write(json_encode(
//            [
//                'status' => 500,
//                'error' => $e->getMessage(),
//                'datas' => ''
//            ]
//        ));
        return json_encode($oUser);
//    }
}

處理資料


測試介面
$res = curl_request('http://lumen.local/');
var_dump($res);
echo '<hr/>';
function curl_request($url,$data=''){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    if($data){
        curl_setopt ( $curl, CURLOPT_POST, 1 );
        curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
    }
    $content = curl_exec($curl);
    return $content;
}