Laravel
  • Introduction
  • 常用語法
  • 結合Vue.js從無到有架設一個網站
  • 透過CMD新增File
  • Schedule
  • Annotation
  • Super Variable
  • API的設定
  • Create 新專案
  • Raw Expressions
  • Import - Export
  • JWT 外加 登入功能
  • Carbon
  • Storage 保存文件
  • Require_once
  • 設定apache將laravel專案run起來
  • 有關leftjoin的用法
  • Advanced Join Clauses
  • Call API方式
  • 產生新的APP_KEY
  • 程序優化技巧
  • composer update VS 手動丟檔
  • 全域變數被使用過
  • FB的SDK
Powered by GitBook
On this page
  • 它不同於ㄧ般的routes
  • ㄧ般的Routes
  • API的特殊

Was this helpful?

API的設定

它不同於ㄧ般的routes

ㄧ般的Routes

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

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

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

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

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

API的特殊

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

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。

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

這是API回傳的樣子

return $result;
PreviousSuper VariableNextCreate 新專案

Last updated 5 years ago

Was this helpful?