Laravel Basic Controllers

Controllers group relate request handling logic into a single class. They are stored in the app/Http/Controllers directory.

Basic Controllers

Defining Controllers:

The controllers extend the base controller class included with Laravel.

The base class provides a few uses to obtain methods such as the middleware method, which we used to attach the middleware to controller actions:

<?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 View
      */
     public function show($id)
     { 
         return view('user.profile', ['user' => User::findOrFail($id)]);
     }
 } 

We can easily define a route to this controller:

Route::get(‘user/{id},’ ‘UserController@show’);

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

The route parameters will also be passed with this method.

  • Controllers are not required to extend a base class. On the other hand, we will not have access to convenience features such as the middleware, validate, and dispatch methods.

Controllers & Namespaces:

We did not need to specify the full controller namespace while defining the controller route.

The RouteServiceProvider loads our route files within a route group which contains the namespace, we can specify the portion of the class name that comes after the App\Http\Controllers portion of the namespace.

If we choose to divide our Controllers into the App\Http\Controllers directory, it uses the specific class name that is related to the App\Http\Controllers root namespace.

If our full controller class is App\Http\Controllers\Photos\AdminController, we should register routes to the controller like so:

Route::get(‘foo’, ‘Phots\AdminController@method’);

Single Action Controllers:

If we would like to define a controller that are only handles a single action, we place a single _invoke method on the controller:

<?php
 namespace App\Http\Controllers;
 use App\User;
 use App\Http\Controllers\Controller;
 class ShowProfile extends Controller 
 {
 /**
 * Show the profile for the given user. 
 *
 * @param  int  $id
 * @return View
 */ 
 public function __invoke($id)
 {
 return view('user.profile', ['user' => User::findOrFail($id)]); 
 }
 } 

When we are registering routes for a single action controllers, we do not need to specify a method:

Route::get('user/{id}', 'ShowProfile');

We can generate an invokable controller by using the --invokable option of the make:controller Artisian command:

php artisian make:controller ShowProfile --invokable


Related Topics

Laravel Templating Inheritance

Introduction: The Blade is a PHP Templating Engine. It grabs the PHP code and makes it easy for us to implement in HTML. The Blade is a simple, and powerful Templating Engine provided by...

4 minutes read.

Laravel vs Other Frameworks

Laravel is one of the top listing PHP framework. Some of the reasons which make Laravel top listing PHP framework that are as follows: Authorization Technique.Object-Oriented Libraries.Artisan.MVC Support.Security.Database Migration.Database Template Engine. In this Pie Chart distribution, we...

6 minutes read.

Laravel Creating Views

Creating Views Views hold the HTML served by our application, and it separates our controller/application logic with the help of presentation logic. Views are stored inside the resources/views directory. A simple view...

2 minutes read.

Database Migrations

Database Migrations in Laravel Introduction Migrations are version control for our database. It allows our team to modify and share the application`s database. Migrations are paired with schema builder to build an application`s database...

2 minutes read.

CSS Icons

CSS Icons       CSS icons are specified like a symbol or image used inside a computer interface assign to any element. CSS icons are the graphical depiction of any program or...

4 minutes read.

Laravel Validation

Laravel provides a different way to validate the application`s incoming data. Laravel base controller class uses a ValidatesRequests that provides an easy method to validate incoming HTTP request with many powerful validation rules. Defining the...

5 minutes read.

Laravel Route Parameters

Required Parameters: We need to capture segments of the URI within our route. We are going to see how to pass parameters through two or many views inside the closure function. Route::get('/home',...

2 minutes read.

Laravel History and Versions

Laravel History Taylor Otwell created the Laravel; it was created as an attempt to provide an advanced alternative to the CodeIgniter framework. The CodeIgniter does not provide several features such as built-in support for...

3 minutes read.

Laravel Advantages

Top 8 Advantages of Laravel The following advantages that Laravel offers, it is based upon designing a web application:- 1. Powerful Authentication: The Laravel PHP framework was developed with a purpose that can help...

2 minutes read.

Laravel Basic Controllers

Controllers group relate request handling logic into a single class. They are stored in the app/Http/Controllers directory. Basic Controllers Defining Controllers: The controllers extend the base controller class included with Laravel. The base class...

2 minutes read.

How to Install Git on Windows

Git Installation Git is a distributed version control system. It is used for tracking changes in source code during development. The goals include speed, data integrity, non-linear workflows, etc. Git was developed in 2005. It is free...

3 minutes read.

Laravel Eloquent Database Relationships

Eloquent Relationships Laravel: Database tables are related to one another. Eloquent manages and work with easy relationships. It supports some different types of relationship, which are as follows: One to oneOne to manyMany...

8 minutes read.

Laravel Tutorial

Laravel Tutorial is a PHP based web-framework; it is used for developing high-end web applications by using significant syntax's. It has a substantial collection of tools and provides application architecture....

5 minutes read.

Raw SQL Queries

Raw SQL Queries: Laravel interact with databases by the variety of database back-ends with raw SQL, the fluent query builder, and the Eloquent ORM. Laravel supports four types of databases: MySQLPostgreSQLSQLiteSQL Server Configuration The database...

3 minutes read.

XAMPP Installation

install xampp on windows XAMPP is a free & open-source stack package which is developed by Apache, mainly it consists of Apache HTTP server, Maria DB database for scripts written in the...

2 minutes read.

Laravel Authentication

Authentication is a process of identifying user credentials. Laravel makes implementing authentication very simple. The authentication configuration file is located at config/auth.php that contains various documented options for adjusting the behavior of the authentication...

4 minutes read.

Laravel Data Views

Passing the Data Views The data should be an array with the key/value pairs while passing the information in the following (return view(‘greeting’, [‘name’ => ‘John’]);) manner. Inside the view, we...

4 minutes read.

Laravel Named Routes

Named Routes Named routes allow the suitable generation of URLs or redirects to specific routes. We specify a name for a route by changing the name method onto the route definition: Syntax:  Route::get(‘user/profile’, function () {...

2 minutes read.

Laravel Forms

CSRF Field For defining an HTML form in our application, we should include a hidden CSRF token field in the form, so that the CSRF protection middleware can validate the request. We use the...

3 minutes read.

Laravel Control Statements

Blade also provides convenient shortcuts for common control structure in PHP, such as conditional statements and loops. These shortcuts provide a very clean working with PHP structures, while also remain familiar to...

3 minutes read.