Support the ongoing development of Laravel.io →
Security Requests Architecture

I am running laravel v5.3.10 and the constructor is as given below, I tried using the auth middleware from the constructor and found that it was not working but the auth middleware works fine when used from the routes/web.php file, then realised that the controller constructor is not being triggered at all if i used a route that made use of the controller functions such as index. What am i missing here, is there something that i should explicitly do to trigger the constructor.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests\CreateAdvertRequest;
use App\Advert;
use Auth;

class AdvertController extends Controller
{
    function __contstruct(){
        dd('die');
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
     
    }
Last updated 2 years ago.
0

#Fix the construct spelling

You need to make it public function __construct(){}, and check your construct spelling, its obvious but you might missed it in first place.

Public access modifier is also needed. Try that and see the outcome. Check here to confirm it.

  1. Check spelling
  2. Make the constructor public
  3. Remove dd('die'), I've run the code and that's the one blocking, because of the flow of the code.

#Why remove dd('die')

The idea of auth is to prevent unauthenticated user from seeing the page. By using dd('die'), you just show the whole page to the unauthenticated user! The flow of the code is interrupted!

public function __construct(){

    $this->middleware('auth');
}

#How to solve?

Use proper 'return' technique, such as this code, or add that dd('die') into the index function:

    public function index(){
    	return view('index.blade.php');
    }

Or if it is not working, do this workaround.

Check if auth is registered

Have you register your 'auth' middleware inside App\Kernel?

    protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    ....
    ];

https://laravel.com/docs/5.3/middleware#registering-middleware

Route casting directly from web.php

Try to use the normal way to cast your middleware, by casting it inside your web.php and see if it is working first hand:

Route::get('/your-route', 'AdvertController@index')->middleware('auth');

Cheers.

Last updated 8 years ago.
0

@XavierIV, thanks, It was the construct spelling mistake, I cant believe i misspelled it, i checked everything a million times but didnt catch the spelling mistake

0

Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in C:\xampp\htdocs\projectsite\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php on line 462 i have the same problem .i am installing microweber which is a multi-featured extendable open source content management system based on the robust Laravel Framework.can anyone help.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.