1. 程式人生 > >laravel學習筆記(二)

laravel學習筆記(二)

  • 路由

HTTP方法:支援http1.1中所有型別傳參方式,get,post,put,delete,options,patch

Route::get($url,$callback);

路由引數:

Route::get('user/{id}',function($id){return 'userid:'.$id});

路由分組:

Route::group(['prefix'=>'admin'],function(){
  Route::get('users',function(){
  //"admin/users"
  })
});

繫結模型

:

 //預設對應關係:user=>表users ,主鍵=>id

 Route::get('api/users/{user}',function(App\user $user){   return $user->email; })

  • 建立控制器

php artisan make:controller PostController

相關:

php artisan 
php artisan help make:controller
  • 建立路由(文章模組)

routes/web.php

//文章列表頁
Route::get('/posts','\App\Http\Controllers\
[email protected]
');
//文章建立
Route::get('/posts/create','\App\Http\Controllers\[email protected]');
Route::post('/posts','\App\Http\Controllers\[email protected]');
//文章詳情頁
Route::get('/posts/{post}','\App\Http\Controllers\[email protected]');
//文章編輯
Route::get('/posts/{post}/edit','\App\Http\Controllers\
[email protected]
');
Route::put('/posts/{post}','\App\Http\Controllers\[email protected]');
//文章刪除
Route::get('/posts/delete','\App\Http\Controllers\[email protected]');
  • 建立控制器具體方法

位置:app/Http/Controllers/PostController.php

public function index(){
     return view("post/index");  //模版渲染
}
public function show(){
    return view("post/show");
}
  • 建立模板

在 resources/views/下建立post資料夾,在其中建立對應的 *.blade.php ,eg: resources/views/post/create.blade.php

  • 模版語法

{{}}
@if @endif
@foreach @endforeach