2017-04-21 95 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.