2

I start creating my application

  1. Install laravel 5 with composer
  2. php artisan migrate ---> ok
  3. php artisan make:auth ---> ok
  4. php artisan make:controller > AboutController --> ok

Result: I have "Home" page (secure page need authentication) and public page, "About"

The route file :

<?php
    Route::get('/', function () {
        return view('welcome');
    });
    Route::auth();
    Route::get('/about', 'AboutController@index');
    Route::get('/home', 'HomeController@index');
    ....
?>

The About controller is like this:

<?php namespace App\Http\Controllers; use App\Http\Requests; use
Illuminate\Http\Request;

    class AboutController extends Controller {

        public function __construct() {

        }

        public function index() {
            return view('about');
        }
    }

This is my about.blade.php file:

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">About</div>

                <div class="panel-body">
                    About page!
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

When I'm logged out:
Everything is fine, and I can access the About page with no problem. The home page needs login, nice.

When I am logged in:
I can access to Home page, that's OK. But when I access the about page, I get an error on the view page something like:

FatalErrorException in AliasLoader.php line 63: Maximum function nesting level of '100' reached, aborting!

If I put: $this->middleware('auth'); in the About page, that's fine, but I don't want to do that.


I have tried all of this:

<?php 
    ............
    //$this->middleware('auth');
    //$this->middleware('auth', ['only' => 'create']);
    //$this->middleware('auth', ['only' => ['create', 'edit', 'destroy']]);
    //$this->middleware('guest', ['except' => 'index']);
    //$this->middleware('guest', ['except' => ['index', 'create']]);
    //$this->middleware('guest');
    ........
?>

But the problem persisted.

Its look like this page needs middleware, so , I did this:

<?php
    namespace App\Http\Controllers;
    use App\Http\Requests;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\Auth;

    class AboutController extends Controller {

        public function __construct() {
            if (Auth::check()) {
                $this->middleware('auth');
            } 
            else {
                $this->middleware('guest');
            }
        }

        public function index() {
            return view('about');
        }
    }
?>

In this case it works fine, so I want to know if my solution is good, or if I'm wrong.

Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
halimus
  • 31
  • 2
  • 1
    Possible duplicate of [Laravel 5.2 Create object FatalErrorException in AliasLoader.php line 63](http://stackoverflow.com/questions/35819097/laravel-5-2-create-object-fatalerrorexception-in-aliasloader-php-line-63) – Chris Forrence Nov 02 '16 at 19:02
  • 1
    Hmm. I just tried to replicate this in Laravel 5.3, but it worked for me. What does your `resources/views/about.blade.php` file look like? You actually shouldn't need the middleware checks in the constructor (I mean, you've definitely got a workaround), but they _should_ be unnecessary in the first place – Chris Forrence Nov 02 '16 at 19:06

1 Answers1

1

Try this:

Route::group(['middleware' => 'auth'], function(){
    Route::get('/about', 'AboutController@index');
    Route::get('/home', 'HomeController@index');
});
user2529934
  • 115
  • 1
  • 5