【问题标题】:Laravel rest pathLaravel 休息路径
【发布时间】:2014-07-27 02:23:39
【问题描述】:

我有这个 route.php 文件:

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

这里是php artisen routes 输出:

+--------+---------------------------+-------------------+-------------------------+----------------+---------------+
| 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 响应?
我试过这个:

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

Error 404

我也试过这个:

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

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

注释 1
我使用 xampp 作为我的 Web 服务器,并且 bets 是包含文件夹。

注 2
我已将 view.php 配置为在公共文件夹中查找视图:

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

注 3
使用卷曲:

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 html curl laravel laravel-4


    【解决方案1】:

    其实答案是php artisen routes命令的结果:

    1. 如果您使用http://localhost/bets/api/loginGET 请求方法(从您的浏览器地址栏或单击链接)发出请求,那么它将点击index 方法。
    2. 如果您使用http://localhost/bets/api/login/createGET 请求方法(从浏览器地址栏或单击链接)发出请求,那么它将点击create 方法。
    3. 如果您使用http://localhost/bets/api/loginPOST 请求方法(使用form 其中action='http://localhost/bets/api/login')发出请求,那么它将命中store 方法。

    4. 如果您使用http://localhost/bets/api/login/idPOST 请求方法(使用form 其中action='http://localhost/bets/api/login/1')发出请求,那么它将命中delete 方法。 1 可以是任何 id,例如 120 等,但您还需要为 DELETE 方法添加隐藏输入,例如:

    要生成表单,您应该使用以下内容:

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

    注意1,它应该是您要删除的模型的id,它基本上可能类似于$modelInstance-&gt;id,因为您可能会将模型从控制器传递到您将生成这个表格。查看Laravel 文档的更多信息。

    【讨论】:

    【解决方案2】:

    经过长时间的调查,我从头开始!现在我将我的主 php 文件 (app.php) 放在 app/view 目录中,并将我的所有 js、css 等文件放在公共目录中。此外,我已将 laravel 视图路径更改为默认路径(app/views),并将 app/route.php 文件更改为:

    <?php
    Route::get('/', function()
    {
        return View::make('app');
    });
    
    Route::group(array('prefix' => 'api'), function()
    {
        Route::resource('login', 'TokenController',
            array('only' => array('index', 'create', 'store', 'destroy')));
    });
    
    App::missing(function($exception)
    {
        return View::make('index');
    });
    

    希望它会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 2016-08-26
      • 2015-02-22
      • 2020-03-12
      • 2019-12-16
      • 1970-01-01
      • 2018-06-10
      相关资源
      最近更新 更多