Laravel Controller



laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

What is Laravel Controller ?

  • A device that controls the transfer of data from a computer to a peripheral device and vice versa.
  • For example, disk drives, display screens, keyboards, and printers all require controllers.
  • In personal computers, the controllers are often single chips.
  • When you purchase a computer, it comes with all the necessary controllers for standard components, such as the display screen, keyboard, and disk drives.
  • If you attach additional devices, however, you may need to insert new controllers that come on expansion boards.
  • Controllers must be designed to communicate with the computer's expansion bus.
  • There are three standard bus architectures for PCs -- the AT bus, PCI (Peripheral Component Interconnect), and SCSI.
  • When you purchase a controller, therefore, you must ensure that it conforms to the bus architecture that your computer uses.
 controllers
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

Basic Controllers:

  • In MVC framework, the letter ‘C’ stands for Controller. It acts as a directing traffic between Views and Models.
  • Here is an example of a basic controller class.
  • All Laravel controllers should extend the base controller class included with the default Laravel installation:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
    /**
    * Show the profile for the given user.
    * @param  int  $id
    * @return Response  */
    public function showProfile($id)
    {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    } 
}
  • All controllers should extend the BaseController class.
  • The BaseController is also stored in the app/controllers directory, and may be used as a place to put shared controller logic.
  • The BaseController extends the framework's Controller class. Now, we can route to this controller action like so:
Route::get('user/{id}', 'UserController@showProfile');
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

Creating a Controller:

  • Open the command prompt or terminal based on the operating system you are using and type the following command to create controller using the Artisan CLI (Command Line Interface).
php artisan make:controller <controller-name> --plain
  • Replace the <controller-name> with the name of your controller. This will create a plain constructor as we are passing the argument - plain.
  • If you don’t want to create a plain constructor, you can simply ignore the argument. The created constructor can be seen at app/Http/Controllers.
  • You will see that some basic coding has already been done for you and you can add your custom coding. The created controller can be called from routes.php by the following syntax.

Syntax

Route::get(‘base URI’,’controller@method’);

Example

  • Step 1 − Execute the following command to create UserController.
php artisan make:controller UserController --plain
  • Step 2 − After successful execution, you will receive the following output.
 example
  • Step 3 − You can see the created controller at app/Http/Controller/UserController.php with some basic coding already written for you and you can add your own coding based on your need.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller {
   //
}

Controller Middleware:

 middeware
  • Middleware may be assigned to the controller's routes in your route files
Route::get('profile', 'UserController@show')->middleware('auth');
  • However, it is more convenient to specify middleware within your controller's constructor.
  • Using the middleware method from your controller's constructor, you may easily assign middleware to the controller's action.
  • You may even restrict the middleware to only certain methods on the controller class:

Assigning Middleware to Route

Route::get('profile', [
   'middleware' => 'auth',
   'uses' => 'UserController@showProfile'
]);
  • Here, we are assigning auth middleware to UserController in profile route.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

Assigning Middleware within Controller’s constructor

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller {
   public function __construct(){
      $this->middleware('auth');
   }
}
  • Here, We are assigning auth middleware using the middleware method in the UserController’s constructor.
Example
  • Step 1 − Add the following lines to the app/Http/routes.php file and save it as routes.php
routes.php
<?php
Route::get('/usercontroller/path',[
   'middleware' => 'First',
   'uses' => 'UserController@showPath'
]);
  • Step 2 − Create a middleware called FirstMiddleware by executing the following line.
php artisan make:middleware FirstMiddleware
  • Step 3 − Add the following code in the handle method of the newly created FirstMiddleware at app/Http/Middleware.
FirstMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;

class FirstMiddleware {
   public function handle($request, Closure $next) {
      echo '<br>First Middleware';
      return $next($request);
   }
}
  • Step 4 −Create a middleware called SecondMiddleware by executing the following line.
php artisan make:middleware SecondMiddleware
  • Step 5 − Add the following code in the handle method of the newly created SecondMiddleware at app/Http/Middleware.
SecondMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;

class SecondMiddleware {
   public function handle($request, Closure $next){
      echo '<br>Second Middleware';
      return $next($request);
   }
}
  • Step 6 − Create a controller called UserController by executing the following line.
php artisan make:controller UserController --plain
  • Step 7 − After successful execution of the URL, you will receive the following output −
 examples
  • Step 8 − Copy the following code to app/Http/UserController.php file.
app/Http/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller {
   public function __construct(){
      $this->middleware('Second');
   }
   public function showPath(Request $request){
      $uri = $request->path();
      echo '<br>URI: '.$uri;
      
      $url = $request->url();
      echo '<br>';
      
      echo 'URL: '.$url;
      $method = $request->method();
      echo '<br>';
      
      echo 'Method: '.$method;
   }
}
  • Step 9 − Now launch the php’s internal web server by executing the following command, if you haven’t executed it yet.
php artisan serve
  • Step 10 − Visit the following URL.
    http://localhost:8000/usercontroller/path
  • Step 11 − The output will appear as shown in the following image.
 control

Resource Controllers:

  • Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code.
  • Using the make:controller Artisan command, we can quickly create such a controller:
  • Just create a controller and Laravel will automatically provide all the methods for the CRUD operations.
  • You can also register a single route for all the methods in routes.php file.
Example
  • Step 1 − Create a controller called photoController by executing the following command.
php artisan make:controller PhotoController
  • Step2 -This command will generate a controller at app/Http/Controllers/photoController.php file.
  • The controller will contain a method for each of the available resource operations.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class photoController extends Controller {
   public function index(){
      echo 'index';
   }
   public function create(){
      echo 'create';
   }
   public function store(Request $request){
      echo 'store';
   }
   public function show($id){
      echo 'show';
   }
   public function edit($id){
      echo 'edit';
   }
   public function update(Request $request, $id){
      echo 'update';
   }
   public function destroy($id){
      echo 'destroy';
   }
}
  • Step 3 − Add the following line of code in app/Http/routes.php file.
app/Http/routes.php
Route::resource('my','photoController');
  • Step 4 − We are now registering all the methods of photoController by registering a controller with resource. Below is the table of actions handled by resource controller.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

Actions Handled By Resource Controller:

Verb Path Action Route Name
GET /photo index photo.index
GET /photo/create create photo.create
POST /photo store photo.store
GET /photo/{photo} show photo.show
GET /photo/{photo}/edit edit photo.edit
PUT/PATCH /photo/{photo} update photo.update
DELETE /photo/{photo} destroy photo.destroy
  • Step 5 − Try executing the URLs shown in the following table.
URL Description Output Image
http://localhost:8000/my Executes index method of photoController.php index
http://localhost:8000/my/create Executes create method of photoController.php create
http://localhost:8000/my/1 Executes show method of photoController.php show
http://localhost:8000/my/1/edit Executes edit method of photoController.php edit

Implicit Controllers:

  • Implicit Controllers allow you to define a single route to handle every action in the controller.
  • You can define it in route.php file with Route:controllermethod as shown below.
Route::controller(‘base URI’,’<class-name-of-the-controller>’);
  • Replace the <class-name-of-the-controller> with the class name that you have given to your controller.
  • The method name of the controller should start with HTTP verb like get or post.
  • If you start it with get, it will handle only get request and if it starts with post then it will handle the post request.
  • After the HTTP verb you can, you can give any name to the method but it should follow the title case version of the URI.
Example
  • Step 1 − Execute the below command to create a controller. We have kept the class name ImplicitController
  • You can give any name of your choice to the class.
php artisan make:controller ImplicitController --plain
  • Step 2 − After successful execution, you will receive the following output −
 implict controllers
  • Step 3 − Copy the following code to app/Http/Controllers/ImplicitController.php file.
app/Http/Controllers/ImplicitController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ImplicitController extends Controller {
   /**
   * Responds to requests to GET /test
   */
   public function getIndex(){
      echo 'index method';
   }
   
   /**
   * Responds to requests to GET /test/show/1
   */
   public function getShow($id){
      echo 'show method';
   }
   
   /**
   * Responds to requests to GET /test/admin-profile
   */
   public function getAdminProfile(){
      echo 'admin profile method';
   }
   
   /**
   * Responds to requests to POST /test/profile
   */
   public function postProfile(){
      echo 'profile method';
   }
}
  • Step 4 − Add the following line to app/Http/routes.php file to route the requests to specified controller.
app/Http/routes.php
Route::controller('test','ImplicitController');

Constructor Injection:

  • The Laravel service container is used to resolve all Laravel controllers.
  • As a result, you are able to type-hint any dependencies your controller may need in its constructor.
  • The dependencies will automatically be resolved and injected into the controller instance.
Example
  • Step 1 − Add the following code to app/Http/routes.php file.
app/Http/routes.php
class MyClass{
   public $foo = 'bar';
}
Route::get('/myclass','ImplicitController@index');
  • Step 2 − Add the following code to app/Http/Controllers/ImplicitController.php file.
app/Http/Controllers/ImplicitController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ImplicitController extends Controller {
   private $myclass;
   
   public function __construct(\MyClass $myclass){
      $this->myclass = $myclass;
   }
   public function index(){
      dd($this->myclass);
   }
}
  • Step 3 − Visit the following URL to test the constructor injection.
  • http://localhost:8000/myclass
  • Step 4 − The output will appear as shown in the following image.
 laravel controller constructor injection

Method Injection:

  • In addition to constructor injection, you may also type - hint dependencies on your controller's action methods.
  • Example
  • Step 1 − Add the following code to app/Http/routes.php file.
app/Http/routes.php
class MyClass{
   public $foo = 'bar';
}
Route::get('/myclass','ImplicitController@index');
  • Step 2 − Add the following code to app/Http/Controllers/ImplicitController.php file.
  • app/Http/Controllers/ImplicitController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ImplicitController extends Controller {
   public function index(\MyClass $myclass){
      dd($myclass);
   }
} 
  • Step 3 − Visit the following URL to test the constructor injection.
  • http://localhost:8000/myclass
  • It will produce the following output −
 laravel controller method injection output

This tutorial is used to learn laravel and also provides guidance on laravel book , laravel books , laravel hosting , laravel programming , laravel server php , laravel server , laravel development server , start laravel server , laravel getting started , laravel programmer , php with laravel for beginners , what is laravel framework in php , up and running with laravel , php framework laravel , getting started with laravel , what is laravel framework , laravel applications , what is laravel in php , laravel hosting , laravel framework , php laravel framework , latest framework in php

Related Searches to Laravel Controller