Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SomeT's avatar
Level 1

How to add a new page viewable only on login?

Using Laravel 5.5.40 LTS with PHP 7.1 based on this: https://laracasts.com/series/laravel-from-scratch-2017/episodes/17 I find this tutorial series does not go into the detail I am after so I am struggling to work out how to do it.

Basically I have created a new page:

newpage.blade.php

I only want it viewable when the user has logged in which means I presume I need to create a new route, ideally I just want to create a route for this because I think that would be easier.

I tried to do stuff like copying and pasting:

Route::get('/home', 'HomeController@index')->name('home');

and changing home to newpage in web.php for the routes but this does not work.

Can anyone please advise what I need to do to basically add a new page to logged in user view only via routes overall please? And advise if I need to add anything in the controller (Add to HomeController.php or create a new one?) additionally if there is anything I have missed in a laracasts video feel free to point me towards the right video.

0 likes
13 replies
Cronix's avatar

Route:

// only authenticated users can use the routes in this group
Route::group(['middleware' => 'auth'], function() {

    // put routes here...
    Route::get('/home', 'HomeController@index')->name('home');
});

//.. any routes here (outside of the route group) are accessible by everyone, auth'd or not...

Now only authenticated users can visit the yoursite.com/home url.

Then in HomeController, the index() method... nothing special. Just do whatever and return a view like normal.

SomeT's avatar
Level 1

Ok this is what I am confused about, if I did not have a page called home, and say it was called newpage.blade.php, how do I add that as a route in respect of not putting this to the homecontroller? Is that even possible? I literally have no idea what I am doing with this, so what you mentioned above I am a bit lost on.

SomeT's avatar
Level 1

Apologies, but I did reference that in my initial question where even from that video series, I can't find specifically where it addresses this issue? Was it mentioned in a particular video? Because even after going through this I believe I might have missed it or not been paying as much attention as I needed to.

Cronix's avatar
Cronix
Best Answer
Level 67

The view should really be triggered by the controller. It will cause other issues if you don't, but to do this:

how do I add that as a route in respect of not putting this to the homecontroller? Is that even possible

You'd change it to this and just return the view from the route. This is really bad practice and will prevent you from doing other things. Really, the controller should return the view. But yes, you can do it...

// only authenticated users can use the routes in this group
Route::group(['middleware' => 'auth'], function() {

    // put routes here...
    Route::get('/home', function() {
        return view('newpage');
    });
});

//.. any routes here (outside of the route group) are accessible by everyone, auth'd or not...

Now going to yoursite.com/home will show the /resources/views/newpage.blade.php view file, if you are logged in

Cronix's avatar

You'd want to watch episode 2 for the basic routing/view stuff. I hope you're watching the whole series from start to end. You need to know it all for it to all work together.

SomeT's avatar
Level 1

Many thanks, what I marked as the correct answer you have to be careful not to put .blade.php as I banged my head against a wall trying to work out why it did'nt work until I realised lol.

Cronix's avatar

Yes, sorry, I fixed it. Just want to say again this will cause other things to not work, as it's not the right way to do it. The route should call a controller method, and that controller method should return view('newpage');

It would be as simple as:

// only authenticated users can use the routes in this group
Route::group(['middleware' => 'auth'], function() {

    // put routes here...
    Route::get('/home', 'PageController@home');
});

//.. any routes here (outside of the route group) are accessible by everyone, auth'd or not...

create the PageController...

php artisan make:controller PageController

Edit /app/Http/Controllers/PageController and add a "home" method:

public function home()
{
    return view('newpage');
}
SomeT's avatar
Level 1

No worries, it helped me to learn because it was wrong. What other things will it break exactly if I do it this way? Also if it was a page title newpage rather than home you would still put home as per your most recent post, respective of it being only viewable when logged in?

Cronix's avatar

What other things will it break exactly if I do it this way?

One thing is you won't be able to cache your routes because it's using a closure, which will make each request to the app slower to resolve, especially with more and more routes added.

https://laravel.com/docs/5.6/controllers#route-caching

Also if it was a page title newpage rather than home you would still put home as per your most recent post, respective of it being only viewable when logged in?

Not sure exactly what you're meaning, but it doesn't matter. You can call things how you want. I was just taking the data you said and making it work. For instance, I wouldn't name the view file "newpage".

SomeT's avatar
Level 1

At the moment I now have:

// only authenticated users can use the routes in this group
Route::group(['middleware' => 'auth'], function() {

    // put routes here...
    Route::get('/train', function() {
        return view('train');
    });
});

I was basically trying to work out how I do the next parts, but I believe on your most recent code part I would just change home to train, apologies tired.

Cronix's avatar

Make a TrainController and put it in there. It's best to learn how to properly do it, or you'll never learn the correct way and cause yourself issues. I even showed you step by step how to do it. It's pretty simple.

SomeT's avatar
Level 1

Yeah it is, once I have gone over it a few times, just tired, it is late here in the UK. Updated profile as well so people can tell what time zone I am in haha. Going to take a break once I have done this.

Please or to participate in this conversation.