> For the complete documentation index, see [llms.txt](https://tobyisme.gitbook.io/laravel/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tobyisme.gitbook.io/laravel/api.md).

# API的設定

### *它不同於ㄧ般的routes*

## ㄧ般的Routes

每個view都會透過Routes來設定路徑給Controller，還有分很多種方式(ex:GET and POST)；

```php
Route::get('/login', function () {
  return \Illuminate\Support\Facades\View::make('login');
});
Route::post('/login', 'Auth\LoginController@login');
```

上面這種都是放在routes底下的web.php裡。

這是ㄧ般routes的controller回傳的樣子

```php
return View('AdvertLocation',compact('condition','order','sort','AdvertLocation'));
```

## API的特殊

因為它這個不ㄧ定會指定回傳到view，它只是直接returnㄧ個值，當然唯一相同的就是也分很多種方式(ex:GET and POST)：

```php
Route::get('GetAdvert','API\GetAdvertController@getAdvert');

Route::get('GetAppAdvert','API\GetAppAdvertController@getAppAdvert');
```

通常我們API會跟普通頁面的controller分開放，所以我會再拉出一個資料夾來放，但是這樣你route就要多加一層連到controller。

這API的routes是放在routes底下的api.php裡，因為如果不放在這裡的話，在輸入網址的時候，最前面還要加上/api才可以連得到，如果想要把這個前綴拿掉的話，可以到`\app\Providers\RouteServiceProvider.php`。

```php
protected function mapApiRoutes()
    {
        Route::group([
            'middleware' => 'api',
            'namespace' => $this->namespace,
            // 'prefix' => 'api',  <===============================這個把他註解掉就OK了
        ], function ($router) {
            require base_path('routes/api.php');
        });
    }
```

這是API回傳的樣子

```php
return $result;
```
