Laravel休息路径 [英] Laravel rest path

查看:187
本文介绍了Laravel休息路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个route.php文件:

I have this route.php file:

Route::group(array('prefix' => 'api'), function () {
    Route::resource(
        'login', 'TokenController',
        ['only' => ['index', 'create', 'store', 'destroy']]
    );
});

这里是 php artisen路线 / p>

Here is php artisen routes output:

+--------+---------------------------+-------------------+-------------------------+----------------+---------------+
| Domain | URI                       | Name              | Action                  | Before Filters | After Filters |
+--------+---------------------------+-------------------+-------------------------+----------------+---------------+
|        | GET|HEAD /                |                   | Closure                 |                |               |
|        | GET|HEAD api/login        | api.login.index   | TokenController@index   |                |               |
|        | GET|HEAD api/login/create | api.login.create  | TokenController@create  |                |               |
|        | POST api/login            | api.login.store   | TokenController@store   |                |               |
|        | DELETE api/login/{login}  | api.login.destroy | TokenController@destroy |                |               |
+--------+---------------------------+-------------------+-------------------------+----------------+---------------+

为了获得TokenController响应,我需要放置什么路径?

我试过了:

What path I need to put in order to get the TokenController response?
I have tried this:

`http://localhost/bets/api/login/create?email=vlio20%40gmail.com&password=vlad1q`

给予错误404

我也尝试过这样:

http://localhost/bets/api/login/create?email=vlio20%40gmail.com&password=vlad1q

显示空白页(响应代码是我的index.php(这是一个空的HTML页面)。

showing an empty page (the response code is my index.php (which is an empty html page).

注释1

我使用xampp作为我的Web服务器,投注是包含的文件夹。

note 1
I am using xampp as my Web server and bets is the containing folder.

note 2

配置view.php以查找公用文件夹中的视图:

note 2
I have configured view.php to look for views in public folder:

'paths' => array(__DIR__.'/../../public')

note 3

使用curl:

note 3
Using curl:

curl -i -H "Accept: application/json" -H "Content-Type: applicatio
n/json" -X GET http://localhost/bets/public/index.php/api/login/create?email=vli
o20@gmail.com&password=vlad1q

返回:

HTTP/1.1 200 OK
Date: Fri, 06 Jun 2014 08:56:58 GMT
Server: Apache/2.4.7 (Win32) OpenSSL/0.9.8y PHP/5.4.22
X-Powered-By: PHP/5.4.22
Content-Length: 723
Content-Type: text/html

<!DOCTYPE html>
<html>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">

<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script src="bower_components/angular/angular.min.js"></script>
<script src="bower_components/angular-route/angular-route.min.js"></script>
<script src="bower_components/bootstrap/dist/js/bootstrap.min.js"></script>

<script src="js/controllers/loginController.js"></script>
<script src="js/services/loginService.js"></script>
<script src="js/controllers/AppController.js"></script>
<head>
    <title>Bets Application</title>
</head>
<body ng-app="betsApp" ng-controller="appCtrl">
<div class="container">
    <div ng-view></div>
</div>
</body>
</html>'password' is not recognized as an internal or external command,
operable program or batch file.


推荐答案

实际上,答案是 php artisen routes 命令:


  1. 如果使用 // localhost / bets / api / login GET 请求方法(从浏览器地址栏或单击链接) code>

  2. 如果您使用 http:// localhost / bets / api / login /使用 GET 请求方法创建(从浏览器地址栏或点击链接),然后它将击中创建方法。

  3. 如果您使用 http:// localhost / bets / api / login POST 请求方法(使用表单其中 action ='http:// localhost / bets / api / login'),那么它将命中 store 方法。

  1. If you make a request using http://localhost/bets/api/login with GET request method (from your browser address bar or clicking a link) then it'll hit index method.
  2. If you make a request using http://localhost/bets/api/login/create with GET request method (from your browser address bar or clicking a link) then it'll hit create method.
  3. If you make a request using http://localhost/bets/api/login with POST request method (using a form where action='http://localhost/bets/api/login') then it'll hit store method.

如果您使用 http:// localhost / bets / api / login / id POST 请求方法(使用表单其中 action ='http:// localhost / bets / api / login / 1'),那么它将命中 delete 方法。 1 可以是任何 id ,例如 1 code> 20 ,因此您还需要为 DELETE 方法添加隐藏输入,如:

If you make a request using http://localhost/bets/api/login/id with POST request method (using a form where action='http://localhost/bets/api/login/1') then it'll hit delete method. The 1 could be any id such as 1 or 20 and so but also you need to add a hidden input for the DELETE method like:

要生成表单,您应该使用omething:

To generate the form you should use omething like:

Form::open(array('route' => array('api.login.destroy', 1), 'method' => 'delete'))

注意 1 它应该是你想要删除的模型的 id ,它基本上可以是类似 $ modelInstance-> id ,因为你可能会将一个模型从你的控制器传递到你将生成这个窗体的视图。详情请参阅 Laravel 文件。

Notice the 1 which should be the id of the model that you want to delete and it basically could be something like $modelInstance->id because you would probably pass a model from your controller to the view where you'll generate this form. Check more on the Laravel documentation.

这篇关于Laravel休息路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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