2017-04-21 36 views
1

我有点困惑,我有一个Web应用程序有登录,注册,注销。一些仪表板视图等(CRUD),我也想为这个应用做一个API。Laravel:路由目录中的Api.php

喜欢哪个第三方将使用,可以更新记录,可以删除记录等

其实应该有一些办法可以是由CRUD移动应用程序使用的API。

我知道我们有那些routes/api.php,但是我很困惑,何时使用它。请解释场景,我是空白的。

更新:

方案

,申请意见,认证系统等,怎样一个Android应用程序将能够在同一个应用程序执行CRUD操作

+0

我认为如果您能够提供一个简单的场景以及您希望获得的结果,那么我们可以参考它会更好。 –

+0

@AntonisTsimourtos问题已更新。 – Gammer

回答

1

1.web路由使用会话状态,CSRF保护。这是否意味着api路由不使用会话状态,CSRF保护?

所有可能但不是必需的。您仍然可以使用会话等,但这是违反REST原则的。

2.laravel 5.3使用独立的web和api路由,有没有什么优势?

这只是为了您的方便。在Laravel 5.2中,你需要为['web']或['api']这样的路由指定中间件,但它不再需要。在存储在分离文件中的5.3路由中,不需要指定路由中间件。

0
If you are specifying routes in api.php, you will need to use the auth:api middleware. For example: 

Route::group(['middleware' => ['auth:api']], function() { 
     Route::get('/test', function (Request $request) { 
      return response()->json(['name' => 'test']); 
     }); 
    }); 

Notes about Token auth and Laravel 5.3: 

If you've setup laravel's default auth system, you will also need to add a column for api_token to the user table. If you are using DB seeders, you might want to add something like: $table->char('api_token', 60)->nullable(); to your users table seeder. Alternatively just add the column manually and fill that column with a random 60-char key. 
When making the request, you can add the api_token as a URL/Querystring parameter like so: domain.com/api/test?api_token=[your 60 char key]. You can also send the key as a header (if using Postman or similar), i.e: Header: Authorization, Value: Bearer [your 60 char key]. 
I order to get a useful error if the token is incorrect, also send the following header with all requests: Header: Accept, Value: application/json. This allows the expectsJson() check in the unauthenticated() function inside App/Exceptions/Handler.php to work correctly.