Jake
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Laravel Controllers > 處理相關的一系列相關的 request 邏輯,ex: UserController 處理 顯示、建立、更新和刪除等等 https://laravel.com/docs/8.x/controllers # 編輯 Controllers ## basic 所有 controller 都會繼承一個 base controller `App\Http\Controllers\Controller` ```php= // controller <?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\Models\User; class UserController extends Controller { /** * Show the profile for a given user. * * @param int $id * @return \Illuminate\View\View */ public function show($id) { return view('user.profile', [ 'user' => User::findOrFail($id) ]); } } // route use App\Http\Controllers\UserController; Route::get('/user/{id}', [UserController::class, 'show']); ``` ## Single Action Controllers 當 controller 非常複雜時,可以寫一個 `__invoke` 方法去處理所有動作 ```php= // controller <?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use App\Models\User; class ProvisionServer extends Controller { /** * Provision a new web server. * * @return \Illuminate\Http\Response */ public function __invoke() { // ... } } // route use App\Http\Controllers\ProvisionServer; Route::post('/server', ProvisionServer::class); // artisan 建立 invoke controller php artisan make:controller ProvisionServer --invokable ``` # Controller Middleware ## 在 route ```php= Route::get('profile', [UserController::class, 'show'])->middleware('auth'); ``` ## 在 controller 檔案裡 ```php= class UserController extends Controller { /** * Instantiate a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); $this->middleware('log')->only('index'); $this->middleware('subscribed')->except('store'); } } ``` ## inline middleware 不需要定義整個 middleware 檔案 ```php= $this->middleware(function ($request, $next) { return $next($request); }); ``` # Resource Controllers 想像每個 eloquent model 都是一個資源,使用者會對它進行 CRUD 的動作,也因為有這些動作 route 應該也要有相對項的路徑,所以 laravel 提供一個簡易的方式產生 resource controller ``` //artisan php artisan make:controller PhotoController --resource // 產生的檔案在 app/Http/Controllers/PhotoController.php //在 route 裡 單一 resource 的寫法 use App\Http\Controllers\PhotoController; Route::resource('photos', PhotoController::class); //在 route 裡 多個 resource 的寫法 Route::resources([ 'photos' => PhotoController::class, 'posts' => PostController::class, ]); ``` Resource Controller 提供的 actions |Verb|URI|Action|Route Name| |-|-|-|-| |GET|/photos|index|photos.index| |GET|/photos/create|create|photos.create| |POST|/photos|store|photos.store| |GET|/photos/{photo}|show|photos.show| |GET|/photos/{photo}/edit|edit|photos.edit| |PUT/PATCH| /photos/{photo}|update|photos.update| |DELETE|/photos/{photo}|destroy|photos.destroy| 客製化當找不到 resource controller 的 eloquent model 時的 404 寫法 ```php= use App\Http\Controllers\PhotoController; use Illuminate\Http\Request; use Illuminate\Support\Facades\Redirect; Route::resource('photos', PhotoController::class) ->missing(function (Request $request) { return Redirect::route('photos.index'); }); ``` 建立 controller 順便產生 model ``` php artisan make:controller PhotoController --model=Photo --resource ``` 建立 controller 順便產生 model 和 form request ``` php artisan make:controller PhotoController --model=Photo --resource --requests ``` ## Partial Resource Routes 當不是要所有的 resource action 都要要用到 controller 時 ```php= use App\Http\Controllers\PhotoController; //只有 Route::resource('photos', PhotoController::class)->only([ 'index', 'show' ]); //排除 Route::resource('photos', PhotoController::class)->except([ 'create', 'store', 'update', 'destroy' ]); ``` ### API Resource Routes 如果只需要將 laravel 當作提供只提供 api 框架那 route 的部份可以用`apiResource`,它會排除[上面提到的 action ](/5IkX0waITXKttvqBbOEM2w?view#Actions-Handled-By-Resource-Controller) create 和 edit 動作 ```php= // route use App\Http\Controllers\PhotoController; Route::apiResource('photos', PhotoController::class); ``` 多個 apiResources 的寫法 ```php= Route::apiResources([ 'photos' => PhotoController::class, 'posts' => PostController::class, ]); ``` 產生 apiResource Controller ```php= php artisan make:controller PhotoController --api ``` ## Nested Resources 有多個資源的 route ```php= use App\Http\Controllers\PhotoCommentController; Route::resource('photos.comments', PhotoCommentController::class); ``` 上面的寫法會是這種 route 的路徑 ```php= /photos/{photo}/comments/{comment} ``` ### Shallow Nesting ```php= use App\Http\Controllers\CommentController; Route::resource('photos.comments', CommentController::class)->shallow(); ``` 上面的寫法是會有下面這些 route Verb|URI|Action|Route Name -|-|-|- GET|/photos/{photo}/comments|index|photos.comments.index GET|/photos/{photo}/comments/create|create|photos.comments.create POST|/photos/{photo}/comments|store|photos.comments.store GET |/comments/{comment}|show|comments.show GET|/comments/{comment}/edit|edit|comments.edit PUT/PATCH|/comments/{comment}|update|comments.update DELETE|/comments/{comment}|destroy|comments.destroy ## 命名 Resource Routes ```php= se App\Http\Controllers\PhotoController; Route::resource('photos', PhotoController::class)->names([ 'create' => 'photos.build' ]); ``` ## Naming Resource Route Parameters 陣列裡需要 resource names 和 parameter names ```php= use App\Http\Controllers\AdminUserController; Route::resource('users', AdminUserController::class)->parameters([ 'users' => 'admin_user' ]); ``` 上面寫法會是下面這樣的 route ```php= /users/{admin_user} ``` ## Scoping Resource Routes 可能 child resource 是用不同的欄位去找可以有這種寫法 ```php= use App\Http\Controllers\PhotoCommentController; Route::resource('photos.comments', PhotoCommentController::class)->scoped([ 'comment' => 'slug', ]); ``` 上面寫法會是下面這種 route ```php= /photos/{photo}/comments/{comment:slug} ``` 這個 route 會去撈 photo 還有和它相關的 一個comment ## Localizing Resource URIs 預設 Route::resource 會用英文的動詞,假如你要自己定義 create 和 edit 等等相關的動詞,可以用 Route::resourceVerbs 方法去實踐在 `App\Providers\RouteServiceProvider` 裡 ```php= public function boot() { Route::resourceVerbs([ 'create' => 'crear', 'edit' => 'editar', ]); // ... } ``` `Route::resource('fotos', PhotoController::class)` 就會產生下面的 route ``` /fotos/crear /fotos/{foto}/editar ``` ## Supplementing Resource Controllers 要增加其它 route 要記得在 resource route 之前 ```php= use App\Http\Controller\PhotoController; Route::get('/photos/popular', [PhotoController::class, 'popular']); Route::resource('photos', PhotoController::class); ``` # Dependency Injection & Controllers [service container ](https://laravel.com/docs/8.x/container) 會去解析所有 contollers,所以你可以 type-hint 任何相依性在 constructor ```php= <?php namespace App\Http\Controllers; use App\Repositories\UserRepository; class UserController extends Controller { /** * The user repository instance. */ protected $users; /** * Create a new controller instance. * * @param \App\Repositories\UserRepository $users * @return void */ public function __construct(UserRepository $users) { $this->users = $users; } } ``` ## Method Injection 除了 constructor injection ,你或許會 type-hint dependencies 在你的 controller methods. 比較常見的是 Illuminate\Http\Request 實例在 controller method ```php= <?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { /** * Store a new user. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $name = $request->name; // } } ``` ###### tags: `2021` `laravel` `controller` `route` `injection` `resource` {%hackmd BJrTq20hE %}

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully