Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel controller construct

Tags:

php

laravel

I started with laravel few days ago and I'm facing this issue:

The NO is never returned!

This is Controller, do you have any idea why?

  Class TestController extends BaseController {

    public function __construct()
    {
        if (!Auth::check()) return 'NO';
    }

    public function test($id)
    {   
        return $id;
    }
}
like image 661
Raggaer Avatar asked Jul 17 '13 10:07

Raggaer


People also ask

What is construct in Laravel?

The constructor is a method that is called whenever a new instance of a (PHP) class is instantiated. This is object oriented programming 101. The code you shared will apply the auth:instructor middleware on all controller actions (in that class) except the action called attendanceShowToStudent .

How do I manually create a Laravel 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). Replace the <controller-name> with the name of your controller. This will create a plain constructor as we are passing the argument — plain.

What is a controller in Laravel?

A Controller is that which controls the behavior of a request. It handles the requests coming from the Routes. In Laravel, a controller is in the 'app/Http/Controllers' directory. All the controllers, that are to be created, should be in this directory.

What is constructor injection in Laravel?

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.

How to resolve all dependencies of a Laravel controller?

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. Step 1 − Add the following code to app/Http/routes.php file.

How to create a MyController in Laravel?

Step 1 − Create a controller called MyController by executing the following command. app/Http/Controllers/MyController.php file. app/Http/Controllers/MyController.php Step 3 − Add the following line of code in app/Http/routes.php file. Step 4 − We are now registering all the methods of MyController by registering a controller with resource.

What is __construct() function in Laravel?

function __construct () is a PHP magic function. It is a simple constructor function which is called when we create an object of that Class. It is used in laravel because laravel is a framework of PHP. What's the difference between Laravel model functions “create” and “insert”?


2 Answers

<?php

class BaseController extends Controller {

    public function __construct()
    {
        // Closure as callback
        $this->beforeFilter(function(){
            if(!Auth::check()) {
                return 'no';
            }
        });

        // or register filter name
        // $this->beforeFilter('auth');
        //
        // and place this to app/filters.php
        // Route::filter('auth', function()
        // {
        //  if(!Auth::check()) {
        //      return 'no';
        //  }
        // });
    }

    public function index()
    {
        return "I'm at index";
    }
}
like image 159
Andreyco Avatar answered Oct 18 '22 03:10

Andreyco


For laravel 5.x:

public function __construct()
{
    $this->middleware(function(){
        if (!Auth::check()) return 'NO';
    });        
}
like image 1
manuelpgs Avatar answered Oct 18 '22 03:10

manuelpgs