Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel - home route

Tags:

php

laravel

I'm learning Laravel, and for my first project I'd like to create my portfolio. However, the first task I have to do is confusing me.

So I created my templates, layout.blade.php and home.blade.php. That makes sense to me, but now how do I tell Laravel, or how do I route to home.blade.php?

I'm looking for an explanation rather then just code. I'm trying to learn.

like image 585
devs Avatar asked Dec 06 '25 20:12

devs


1 Answers

Actually, a view in MVC application is just a part of the application and it's only for presentation logic, the UI and one doesn't call/load a view directly without the help of another part (controller/function) of the application. Basically, you make a request to a route and that route passes the control over to a controller/function and from there you show/load the view. So it's not a tutorial site and it's also not possible to explain about MVC here, you should read about it and for Laravel, it's best place to understand the basics on it's documentation, well explained with examples, anyways.

In case of Laravel, you should create a controller/class or an anonymous function in your apps/routes.php file and show the view from one of those. Just follow the given instruction step by step.

Using a Class:

To create a route to your Home Controller you should add this code in your app/routes.php

// This will call "showWelcome" method in your "HomeController" class
Route::any('/', array( 'as' => 'home', 'uses' => 'HomeController@showWelcome' ));

Then create the HomeController controller/class (create a file in your controllers folder and save this file using HomeController.php as it's name) then paste the code given below

class HomeController extends BaseController {
    public function showWelcome()
{
        // whatever you do, do it here
        // prepare some data to use in the view (optional)
        $data['page_title'] = 'Home Page';
        // finally load the view
        return View::make('home', $data);
    }
}

If you have {{ $title }} in your home.blade.php then it'll print Home Page. So, to use a view you need a controller or an anonymous function and load the view from the controller/function.

Using an anonymous function:

Also, you can use an anonymous function instead of a controller/class to show the view from directly your route, i.e.

Route::any('/', function(){
    // return View::make('home'); 
    // or this
    $data['page_title'] = 'Home Page'; // (optional)
    return View::make('home', $data);
});

Using this approach, whenever you make a request to the home page, Laravel will call the anonymous function given in/as route's callback and from there you show your view.

Make sure to extend the the master/main layout in sub view (home):

Also, remember that, you have following at the first line of your home.blade.php file

@extends('layouts.layout')

It looks confusing, you may rename the main layout (layout.blade.php) to master.blade.php and use following in your home.blade.php instead

@extends('layouts.master')

Read the doc/understand basics:

You should read Laravel's documentation properly, (check templates to understand blade templating) and also read some MVC examples, that may help you too understand the basics of an MVC framework (you may find more by googling) and some good posts about MVC on SO.

like image 158
The Alpha Avatar answered Dec 09 '25 14:12

The Alpha