Skip to content

Controllers

Benjamin Khalife edited this page Sep 11, 2022 · 4 revisions

Introduction

Instead of defining all of your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes. Controllers can group related request handling logic into a single class. Controllers are stored in the app/controllers directory.

Basic Controllers

<?php
namespace App\Controllers;

class IndexController
{

  public function helloWorld()
  {
    return view('welcome' , [ 'name' => 'Webrium Framework' ] );
  }

}

You can define a route to this controller action like so:

Route::get('hello', 'indexController->helloWorld');

Now, when a request matches the specified route URI, the helloWorld method on the indexController class will be executed.

Action Controllers

<?php
namespace App\Controllers;

use App\Models\User;

class UserController
{


  public function show()
  {
    $user_id = input('user_id');
    $user = User::find($user_id);

    // ..
  }


}
Clone this wiki locally