Laravel 4 Angular.js的RESTful API [英] Laravel 4 RESTful API with Angular.js

查看:185
本文介绍了Laravel 4 Angular.js的RESTful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 的RESTful API 基础与应用的 Laravel 4 角。 JS
该应用程序的CRUD过程是由angularjs $ HTTP服务处理。

后端侧(Laravel 4):

路由:应用程序/ routes.php文件

  // .....
路线::组(阵列('preFIX'=>'API / V1','前'=>'auth.basic'),功能()
{
    // ....
    路线::资源(页,PagesController');
    // ....
});
// .....

控制器:应用程序/控制器/ API / PageController.php

 < PHP// .....
类PagesController扩展BaseController {
  // ......
  公共功能更新(的$ id){
        $页=页::发现($ ID);        如果(支持::获得(标题))
        {
            $页面级>标题=支持::获得(标题);
        }        如果(支持::获取('塞'))
        {
            $页面级>蛞蝓=支持::得到('塞');
        }        $页面级>保存();        返回响应:: JSON(阵列(
            '错误'=>假,
            '消息'= GT; 页更新),
            200
        );
  }
  // ......
}

电话:卷曲

此更新功能可以用卷曲的方法也可以访问。

 卷曲-i -X​​ PUT --user管理:管理员-d称号=更新的标题本地主机/ laravel / index.php文件/ API / V1 /页/ 2

前端:HTML

 <! - 上衣code  - >
<! - 从添加/编辑页面 - >
<窗​​体类=形横角色=形式NG-秀=编辑NG提交=的updatePage(进入)>  <! - 页面标题 - >
  < D​​IV CLASS =表单组>
    <标签类=COL-LG-2控制标签>页面标题< /标签>
    < D​​IV CLASS =COL-LG-4>
      <输入类型=文本级=表格控VALUE ={{entry.title}}NG模型=entry.title>
    < / DIV>
  < / DIV>  <! - 弹头 - >
  < D​​IV CLASS =表单组>
    <标签类=COL-LG-2控制标签>弹头< /标签>
    < D​​IV CLASS =COL-LG-4>
      <输入类型=文本级=表格控VALUE ={{entry.slug}}NG模型=entry.slug>
    < / DIV>
  < / DIV>
  < D​​IV CLASS =表单组>
    < D​​IV CLASS =COL-LG-偏移2 COL-LG-10>
      <按钮式=提交级=BTN BTN-小学>更新和LT; /按钮>
    < / DIV>
  < / DIV>< /表及GT;
<! - 底code - >

客户端:angularjs

  // ......
功能pageCtrl($范围,$ HTTP,数据){
  // .........
  $ scope.updatePage =功能(输入){
        $ HTTP({方法:把',网址:Data.root_path +'API / V1 /页/+ ID})。
        成功(功能(数据,状态,头,配置){
            //
        })。
        错误(功能(数据,状态,头,配置){
            //
        });  }
  // .........
}

问:


  1. 如何通过我的表单数据(多个值)到$ http.put要求
    在这里?

  2. 如何访问Laravel 4控制器PUT请求的数据?能够
    我使用的输入::得到()?


解决方案

需要一些更新在HTML获得页面的ID进行更新。添加以下HTML中的形式。

 <输入类型=隐藏NG-模式=entry.idVALUE =entry.id/>

然后换角度脚本,

  $ scope.updatePage =功能(输入){    $ http.put(Data.root_path +'API / V1 /页/+ entry.id,进入)
     .success(功能(数据,状态,头,配置){
        //
     })
     .error(功能(数据,状态,头,配置){
        //
     });
 }

而在你的Laravel控制器,

 公共功能更新(的$ id){  $页=页::发现($ ID);  $输入= $输入=输入::所有();   如果($输入['标题'])
   {
      $页面级>标题= $输入['标题'];
   }   如果($输入['塞'])
   {
     $页面级>蛞蝓= $输入['塞'];
   }  $页面级>保存();  返回响应:: JSON(阵列(
    '错误'=>假,
    '消息'= GT; 页更新),
    200
  );
}

I have a RESTful API based application with Laravel 4 and Angular.js. The application's CRUD processes are handled by angularjs $http service.

The Backend Side (Laravel 4):

Routing : app/routes.php

//.....
Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
    //....
    Route::resource('pages', 'PagesController');
    //....
});
//.....

Controller : app/controllers/api/PageController.php

<?php

//.....
class PagesController extends BaseController {
  //......
  public function update($id) {
        $page = Page::find($id);

        if ( Request::get('title') )
        {
            $page->title = Request::get('title');
        }

        if ( Request::get('slug') )
        {
            $page->slug = Request::get('slug');
        }

        $page->save();

        return Response::json(array(
            'error' => false,
            'message' => 'Page Updated'),
            200
        );
  }
  //......
}

Calling : cURL

This update function can be accessed using cURL method also.

curl -i -X PUT --user admin:admin -d 'title=Updated Title' localhost/laravel/index.php/api/v1/pages/2

Front-end : HTML

<!-- Top Code -->
<!-- From to Add/Edit Pages -->
<form class="form-horizontal" role="form" ng-show="edit" ng-submit="updatePage(entry)">

  <!-- Page Title -->   
  <div class="form-group">
    <label class="col-lg-2 control-label">Page Title</label>
    <div class="col-lg-4">
      <input type="text" class="form-control" value="{{entry.title}}" ng-model="entry.title">
    </div>
  </div>

  <!-- Slug -->
  <div class="form-group">
    <label class="col-lg-2 control-label">Slug</label>
    <div class="col-lg-4">
      <input type="text" class="form-control" value="{{entry.slug}}" ng-model="entry.slug">
    </div>
  </div>


  <div class="form-group">
    <div class="col-lg-offset-2 col-lg-10">
      <button type="submit" class="btn btn-primary">Update</button>
    </div>
  </div>

</form>
<!-- Bottom Code -->

Client-side : angularjs

// ......
function pageCtrl($scope, $http, Data) {
  //.........
  $scope.updatePage = function(entry) {


        $http({method: 'PUT', url: Data.root_path + 'api/v1/pages/'+id}).
        success(function(data, status, headers, config) {
            //
        }).
        error(function(data, status, headers, config) {
            //
        });

  }
  //.........
}

Question:

  1. How can I pass my form data(more than one values) to the $http.put request here ?
  2. How can I access the PUT request data in Laravel 4 Controller ? Can I use Input::get() ?

解决方案

Need some update in your html to get page id to update. Add the following html inside form.

<input type="hidden" ng-model="entry.id" value="entry.id"/>

Then change angular script to,

 $scope.updatePage = function(entry) {

    $http.put(Data.root_path + 'api/v1/pages/' + entry.id, entry)
     .success(function(data, status, headers, config) {
        //
     })
     .error(function(data, status, headers, config) {
        //
     });
 }

And in your Laravel Controller,

public function update($id) {

  $page = Page::find($id);

  $input = $input = Input::all();

   if ( $input['title'] )
   {
      $page->title = $input['title'];
   }

   if ( $input['slug'] )
   {
     $page->slug = $input['slug'];
   }

  $page->save();

  return Response::json(array(
    'error' => false,
    'message' => 'Page Updated'),
    200
  );
}

这篇关于Laravel 4 Angular.js的RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆