Laravel CRUD和API webservice

时间:2017-08-22 13:01:40

标签: php laravel web-services api

我是初学PHP开发人员,我使用Laravel 5.4框架实现CRUD,到目前为止一切正常。

但是我当时正试图让这个代码在网站和移动设备上运行,所以我开始了解网络服务和它的协议,比如Rest,Soap,我成功地设法与他们合作并建立小自学的大小脚本,事情变得更好。

当我尝试应用我在CRUD上学到的东西时,我没有链接来构建我的代码而只是路由和a api.php,web.php文件,我不知道在哪里构建我的服务器或客户端脚本以及如何在laravel中链接它们,即使我设法在本机php中实现这一点,但是在laravel中我有点困惑我在网上冲浪,发现实际上对我没有任何帮助..

我将在(创建新的用户功能)上提供简单的CRUD代码。并且希望任何人可以帮助我或让我开始在不同的项目中使用这种技术。

我的控制器     

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\user;

class AddController extends Controller
{
public function create(){                //create new user page
    return view('add.create');
}
public function store(){              //store new user added by a clint

    $this->validate(request(), [           //validation for request records

        'name' => 'required',
        'email' => 'required',
        'password' => 'required|min:8',
        'password_confirmation' => 'required|same:password'

        ]);



        $user = User::create([           //create new user with the request records

            'name' => request('name'),
            'email' => request('email'),
            'password' =>bcrypt(request('password'))


            ]);


        session()->flash('message','Changes has been Applied');          //flash a succcess message 
        return redirect()->home();           // redirect to home after submitting the new user

}
}

我的路线(不是资源路线,只是原生路线)

 // add new user  routes
 Route::get('add','AddController@create')->middleware('authenticated');
 Route::post('add','AddController@store');

我的模特

是laravel提供的内置User.php模型。

我的视图add.create.blade.php

<!-- this is the view of the add new user tab , extending master layout and it's components-->

@extends('layouts.master')

@section('content')

<div class="col-md-8">

<h3>Enter the Values of the new User</h3>

<form method="POST" action="add">
    {{csrf_field()}}

    <div class="form group">
        <label for="name">*Name:</label>
        <input type="name" class="form-control" id="name" name="name">
    </div>
    <div class="form group">
        <label for="Email">*Email Address:</label>
        <input type="email" class="form-control" id="email" name="email">
    </div>
    <div class="form group">
        <label for="password">*Password:</label>
        <input type="password" class="form-control" id="password" name="password">
    </div>
    <div class="form-group">
        <label for="password confirmation">*Confirm Password:</label>
        <input type="password" class="form-control" id="password_confirmation" name="password_confirmation" >
    </div>
    <br>
    <div class="form-group">
        <button type="submit" class="btn btn-primary">Add User</button>
    </div>


    @include('layouts.errors')


</form>

</div>

@endsection

这是我迄今为止所达到的目标,我希望如果有人告诉我如何将api应用于此代码以使其在移动设备上运行,我非常感谢您提前提供任何帮助。

1 个答案:

答案 0 :(得分:1)

现在你的控制器返回一个视图文件 - 带有一些PHP变量的HTML模板。您的API不需要HTML代码,所以首先,您应该摆脱它。

您的API路由(在API案例路由中称为&#39;端点&#39;)应以结构化格式返回信息 - 如果您使用的是REST API,则应以JSON格式返回数据(http://jsonapi.org/examples/ - 如果你正在使用SOAP,那么JSON响应的例子也是如此 - 响应应该是XML(我第一次建议你使用REST,因为构建REST API要简单得多)。

一个好的做法是在构建响应时使用变形金刚(例如,看看https://medium.com/@haydar_ai/how-to-start-using-transformers-in-laravel-4ff0158b325f)。

您还应该在api.php文件中创建端点 - 此文件专门用于满足此需求。别忘了这个文件中的所有路由都有&#39; api&#39;的前缀。

https://laracasts.com/series/incremental-api-development中有一个很棒的视频系列,其中构建了简单的REST api。