• lumen Rest API 起步


    lumen Rest API 起步

    修改项目文件

    .env

    DB_DATABASE=<数据库名>
    DB_USERNAME=<数据库用户名>
    DB_PASSWORD=<数据库密码>
    

    bootstrap/app.php

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

    数据库迁移

    创建数据表

    php artisan make:migration create_table_users --create=users
    

    定义数据表
    database/migrations/迁移文件

    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
    });
    

    运行迁移

    php artisan migrate
    

    创建模型

    接下来我们在app目录下创建模型文件User.php

    namespace App;
    
    use IlluminateAuthAuthenticatable;
    use IlluminateContractsAuthAccessAuthorizable as AuthorizableContract;
    use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
    use IlluminateDatabaseEloquentModel;
    use LaravelLumenAuthAuthorizable;
    
    class User extends Model implements AuthenticatableContract, AuthorizableContract
    {
        use Authenticatable, Authorizable;
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $table = 'users';
    
        protected $fillable = [
            'id', 'name',
        ];
    
        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = [];
    
        public $timestamps = false;
    }
    

    创建控制器

    然后创建控制器文件app/Http/Controllers/UserController.php

    namespace AppHttpControllers;
    
    use AppUser;
    use DB;
    use IlluminateHttpRequest;
    
    class UserController extends Controller
    {
        public function createUser(Request $request)
        {
            $user = User::create($request->all());
            return response()->json($user);
        }
    
        public function updateUser(Request $request,$id)
        {
            $user = User::find($id);
            $user->name = $request->input('name');
            $user->save();
    
            return response()->json($user);
        }
    
        public function deleteUser($id)
        {
            $user = User::find($id);
            $user->delete();
    
            return response()->json('删除成功');
        }
    
        public function index($id = null)
        {
            if (!empty($id)) {
                $users = User::find($id);
            }else{
                $users = User::all();
            }
            return response()->json($users);
        }
    
        public function hello()
        {
            return 'hello';
        }
    }
    

    定义路由

    修改文件bootstrap/app.php

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

    打开app/Http/routes.php并添加路由

    $router->get('/hello', array(
        'uses' => 'UserController@hello'
    ));
    
    $router->group(['prefix' => 'api'], function() use ($router){
        $router->post('person', 'UserController@createUser');
        $router->put('person/{id}','UserController@updateUser');
        $router->delete('person/{id}','UserController@deleteUser');
        $router->get('person[/{id}]','UserController@index');
    });
    

    测试API

    curl -i -X POST -H "Content-Type:application/json" http://www.lelumen.test/api/person -d '{"id":2,"name":"test1"}'
    curl -i -X POST -H "Content-Type:application/json" http://www.lelumen.test/api/person -d '{"name":"test22"}'
    
    curl -H "Content-Type:application/json" http://www.lelumen.test/api/person/1 -X PUT -d '{"name":"ttt"}'
    
    curl -H "Content-Type:application/json" http://www.lelumen.test/api/person -X GET
    
    curl -H "Content-Type:application/json" http://www.lelumen.test/api/person/1 -X GET
    
    curl -X DELETE http://www.lelumen.test/api/person/1
    

    空格引起的奇葩,阿哈哈

    参考文件

  • 相关阅读:
    IPython notebook(Jupyter notebook)指定IP和端口运行
    spark-2.2.0安装和部署——Spark集群学习日记
    安装Scala-2.11.7——集群学习日记
    hadoop2.7.3在centos7上部署安装(单机版)
    Centos7下面配置静态IP
    css 3列
    css 日历组件(浮雕效果)
    if(!document.getElementById) return false; JS里这句是什么意思?
    网页中图片路径错误时显示默认图片方法
    css3 图片放大缩小闪烁效果
  • 原文地址:https://www.cnblogs.com/jjxhp/p/13363948.html
Copyright © 2020-2023  润新知