• Skip to main content
  • Skip to primary sidebar

Technical Notes Of
Ehi Kioya

Technical Notes Of Ehi Kioya

  • Forums
  • About
  • Contact
MENUMENU
  • Blog Home
  • AWS, Azure, Cloud
  • Backend (Server-Side)
  • Frontend (Client-Side)
  • SharePoint
  • Tools & Resources
    • CM/IN Ruler
    • URL Decoder
    • Text Hasher
    • Word Count
    • IP Lookup
  • Linux & Servers
  • Zero Code Tech
  • WordPress
  • Musings
  • More
    Categories
    • Cloud
    • Server-Side
    • Front-End
    • SharePoint
    • Tools
    • Linux
    • Zero Code
    • WordPress
    • Musings
Home » Programming » Laravel » Controllers In Laravel – An Introduction

Controllers In Laravel – An Introduction

April 5, 2020 by Chinomnso Leave a Comment

Routing is a mechanism that Laravel uses to decide which method is to be executed when a request is made to a controller class. A large application could have lots of request-handling logic defined. Storing these in a single file could make the code hard to maintain.

It is usually better to keep the code for all of this logic organized by making use of Controller classes. These are stored in the App/Http/Controllers directory.

A default Laravel installation has a base controller that controls other controllers. So, all controllers should extend the default controller. The code below is an example of what a basic controller in Laravel might look like:

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;

class ProfileController extends Controller
{
    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function displayProfile($id)
    {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    }
}

To route the controller action above, use the following line of code:

Route::get('user/{id}', 'ProfileController@displayProfile');

When a request is made and it matches the URI in the specified route, the displayProfile method of the ProfileController class will be executed. The route parameters will also be passed to the method.

Controllers In Laravel: Namespaces

In the code above, we did not need to specify the full controller namespace when defining the route of the controller. What we did was to define the portion of the class name that follows App/Http/Controllers.

If you decide to create your controllers in a sub-directory of App/Http/Controllers use a class name that is relative to that directory. You don’t have to use the absolute path of the controller.

So, if your controller class is stored at App\Http\Controllers\Photos\DahsboardController for example, the route should be registered like this:

Route::get('foo', 'Photos\DashboardController@method');

Controllers In Laravel - An Introduction

Single Action Controllers

Sometimes, you may want to create a controller that handles just a single action. To do this, place a single __invoke method in the controller like this:

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;

class DisplayProfile extends Controller
{
    /**
     * Show the profile for the specified user
     *
     * @param  int  $id
     * @return Response
     */
    public function __invoke($id)
    {
        return view('user.profile', ['user' => User::findOrFail($id)]);
    }
}

When registering routes for single action controllers, you do not need to specify the method you want to work with. Here’s an example:

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

That’s it.

This is a primer on controllers in Laravel. I’ll gladly entertain questions on this topic if you have any. You can leave your comments and questions using the comments box below.

If you desire even more details, check out the official Laravel controllers documentation.

Found this article valuable? Want to show your appreciation? Here are some options:

  1. Spread the word! Use these buttons to share this link on your favorite social media sites.
  2. Help me share this on . . .

    • Facebook
    • Twitter
    • LinkedIn
    • Reddit
    • Tumblr
    • Pinterest
    • Pocket
    • Telegram
    • WhatsApp
    • Skype
  3. Sign up to join my audience and receive email notifications when I publish new content.
  4. Contribute by adding a comment using the comments section below.
  5. Follow me on Twitter, LinkedIn, and Facebook.

Related

Filed Under: Backend (Server-Side), Laravel, PHP, Programming, Web Development Tagged With: Controllers, Laravel, PHP, Programming, Web Development

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

25,641
Followers
Follow
30,000
Connections
Connect
14,583
Page Fans
Like

POPULAR   FORUM   TOPICS

  • How to find the title of a song without knowing the lyrics
  • Welcome Message
  • How To Change Or Remove The WordPress Login Error Message
  • The Art of Exploratory Data Analysis (Part 1)
  • Replacing The Default SQLite Database With PostgreSQL In Django
  • How to Implement Local SEO On Your Business Website And Drive Traffic
  • Getting Started with SQL: A Beginners Guide to Databases
  • The Best Web Safe Fonts In Web Development
  • What’s the fuss about HMAC Validation?
  • Data Science Insights: Regression Analysis
  • Recently   Popular   Posts   &   Pages
  • Actual Size Online Ruler Actual Size Online Ruler
    I created this page to measure your screen resolution and produce an online ruler of actual size. It's powered with JavaScript and HTML5.
  • Allowing Multiple RDP Sessions In Windows 10 Using The RDP Wrapper Library Allowing Multiple RDP Sessions In Windows 10 Using The RDP Wrapper Library
    This article explains how to bypass the single user remote desktop connection restriction on Windows 10 by using the RDP wrapper library.
  • WordPress Password Hash Generator WordPress Password Hash Generator
    With this WordPress Password Hash Generator, you can convert a password to its hash, and then set a new password directly in the database.
  • Forums
  • About
  • Contact

© 2021   ·   Ehi Kioya   ·   All Rights Reserved
Privacy Policy