Quick Contact

    Laravel Controller

    Basically Controller is used to handle or control the behavior of request.

    Let’s start with a file suppose “web.php”, Soppose in file you set a route as-:

    	Route::get (‘/’, function () {
    	Return view (‘home’);

    Let’s now structure it a little bit with a controller in between; you don’t need to do logic inside file. So, without login what you could do to create a anew controller and there are two ways how you could do that- you have super fast way or manual way.

    Let’s start it by doing it manual way. In plain php you would go to controllers directory, so let’s open up controllers file by ‘go to HTTP> Controllers> create new file ther’. Let’s create “productscontroller.php” and then start with php codes or php opening tag in Laravel. You need to define your namespace and further code is written as-:

    – Open php tag

    – Write namespace

    – Create class which extends the core controller


    It will work fine but it took too long to proceed.

    another way with “Php artisan method” is easier to go with. Create controller with php artisan is easy and fast.

    So let’s go to terminal and do inside desktop, cd and so on to workspace with command. Create your first project now.

    – First write “php artisan –help”, after enter the command you will see couple commands that you could perform.

    – In order to list all commands write “php artisan list” and you will see complete list of all the php artisan commands with examples.

    – Whenever you want to perform some kind of artisan command and you don’t know the name, use the php artisan list to list them all down, which are then written alphabetically.

    Now you want to make a controller and description is create a new controller class. So let’s write down-:

    Php artisan make: controller (controller name)

    Before it first go back to visual studio code and remove the respective present same name controller. Now go back to env file and give make controller command as-:

    Php artisan make:controller ProductsController

    Now as you see the output controller is successfully created. Now see the output in visual studio, the products controller has been created. Namespace will be set as-:

    And a class is created.

    Before move further, let’s know about some commands in console.

    – Now go back to console and write
    (dash help). After putting this command you will see 2 flags, of shorter version and longer version. First version is basically a shortcut.

    – There are commands shown which have their respective meaning as-:

    Now move to topic-:

    If controller is already built there and you try to built the controller using artisan command as-:

    Php artisan make: controller ProductsController

    And press enter and controller created successfully. But after in next line you will get message that controller already exist.

    – What you would do now, add a new flag with command-

    >php artisan make: controller ProductsController –force

    And the controller will create successfully. But after this it will notify that controller already exist.

    Now move to-:

    How you make function inside controller?

    Simply use the command to create function, in which you will put function name print in echo. You should also define access modifier as public or private as-:


    So now how you call the controller from the Router-:

    So for this go to the file placed in the routes folder. Write

    Route:: get (“path”, “controller file”);

    So this is the respective command to call the controller.


    Apply now for Advanced PHP Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.