Api 路由在 Laravel 5.4 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42872464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:35:52  来源:igfitidea点击:

Api routes not working in laravel 5.4

phplaravelapi

提问by Kuldeep Singh Rathore

I am trying to call an api route from postman like this

我正在尝试像这样从邮递员那里调用 api 路由

http://project.app/api/someroute

http://project.app/api/someroute

But it does not work and through that error

但它不起作用并通过该错误

file_put_contents(/home/vagrant/Code/project/storage/framework/cache/data/a0/c6/a0c65d98bc6f1fe2c35f8ba5fd47c8a3e225165e): failed to open stream: No such file or directory

file_put_contents(/home/vagrant/Code/project/storage/framework/cache/data/a0/c6/a0c65d98bc6f1fe2c35f8ba5fd47c8a3e225165e): failed to open stream: No such file or directory

采纳答案by Alexey Mezenin

Create this directory manually:

手动创建此目录:

/storage/framework/cache

Also, make sure you have:

另外,请确保您拥有:

/storage/framework/views
/storage/framework/sessions

Usually, you'll also need to set the right permissions on the storagedirectory:

通常,您还需要为storage目录设置正确的权限:

chmod -R 755 storage

回答by zechdc

Couple things to check

需要检查的几件事

1) Make sure you add an api_token column to your users database.

1) 确保将 api_token 列添加到用户数据库中。

$table->string('api_token', 60)->unique()

$table->string('api_token', 60)->unique()

2) Add a random 60 character string to that column

2) 向该列添加一个随机的 60 个字符串

3) Add a route to your api.php routes file

3) 在你的 api.php 路由文件中添加一个路由

Route::group(['prefix' => 'v1', 'middleware' => 'auth:api'], function ()
{
    Route::get('test', function(){
        return ['name' => 'zechdc'];
    });
});

Notice I added a prefix of v1. Looks like laravel already adds an 'api' prefix as well.

请注意,我添加了 v1 的前缀。看起来 laravel 也已经添加了一个 'api' 前缀。

4) Visit your url and append your api_token you created for a user. You should see some JSON on your screen at this point.

4) 访问您的 url 并附加您为用户创建的 api_token。此时您应该会在屏幕上看到一些 JSON。

http://localhost/api/v1/test?api_token=wwfaadmcmsvbtldzxlenjxhxakwyriwhrxcbkgfpnlccyhhxexvzfixxjtjq

http://localhost/api/v1/test?api_token=wwfaadmcmsvbtldzxlenjxhxakwyriwhrxcbkgfpnlccyhhxexvzfixxjtjq

Source: https://laracasts.com/discuss/channels/laravel/53-api-routes-auth-middleware-confusion?page=1

来源:https: //laracasts.com/discuss/channels/laravel/53-api-routes-auth-middleware-confusion?page=1