问题
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:
- If you make a request using
http://localhost/bets/api/login
withGET
request method (from your browser address bar or clicking a link) then it'll hitindex
method. - If you make a request using
http://localhost/bets/api/login/create
withGET
request method (from your browser address bar or clicking a link) then it'll hitcreate
method. If you make a request using
http://localhost/bets/api/login
withPOST
request method (using aform
whereaction='http://localhost/bets/api/login'
) then it'll hitstore
method.If you make a request using
http://localhost/bets/api/login/id
withPOST
request method (using aform
whereaction='http://localhost/bets/api/login/1'
) then it'll hitdelete
method. The1
could be anyid
such as1
or20
and so but also you need to add a hidden input for theDELETE
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