Laravel rest path

对着背影说爱祢 提交于 2019-12-08 08:21:14

问题


I have this route.php file:

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

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 |                |               |
+--------+---------------------------+-------------------+-------------------------+----------------+---------------+

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`

giving Error 404.

I have tried also this:

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

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

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

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

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

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

Returning this:

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.

回答1:


Actually the answer is the result of php artisen routes command:

  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.

  4. 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:

To generate the form you should use omething like:

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

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.




回答2:


After long investigation I have started from scratch! Now I placed my main php file (app.php) in the app/view directory and all my js, css etc files in the public dir. Also I have changed the laravel view path to the default path (app/views) and the app/route.php file to be:

<?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');
});

Hope it will help!



来源:https://stackoverflow.com/questions/24071257/laravel-rest-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!