Mark
Mark

Reputation: 3197

Laravel 4 specific view not loading

I am new to laravel and following a tutorial for a basic app. So far the app has a default view layouts/default.blade.php, a partial _partials/errors.blade.php and three other views questions/index.blade.php, users/new.blade.php and users/login.blade.php

The routes are defined like so

// home get route
Route::get('/', array('as'=>'home', 'uses'=>'QuestionsController@get_index'));

//user register get route

Route::get('register', array('as'=>'register', 'uses'=>'usersController@get_new'));

// user login get route

Route::get('login', array('as'=>'login', 'uses'=>'usersController@get_login'));

//user register post route

Route::post('register', array('before'=>'csrf', 'uses'=>'usersController@post_create'));

// user login post route

Route::post('login', array('before'=>'csrf', 'uses'=>'usersController@post_login'));

questions/index.blade.php and users/new.blade.php load fine and within default.blade.php

when I call /login a blank page is loaded not even with default.blade.php. I am guessing that there is a problem in my blade syntax in login.blade.php given the fact that the default.blade.php works on the other routes and as far as I can see everything else is the same but if that was teh case wouldnt the default.blade.php route at least load?

the controller method this route is calling is as follows

<?php

    Class UsersController extends BaseController {

        public $restful = 'true';
        protected $layout = 'layouts.default';

        public function get_login()
        {
            return View::make('users.login')
            ->with('title', 'Make It Snappy Q&A - Login');
        }

        public function post_login()
        {
            $user = array(
                'username'=>Input::get('username'),
                'password'=>Input::get('password')
            );

            if (Auth::attempt($user)) {
                return Redirect::Route('home')->with('message', 'You are logged in!');
            } else {
                return Redirect::Route('login')
                ->with('message', 'Your username/password combination was incorrect')
                ->withInput();
            }
        }
    }
?>

finally login.blade.php

@section('content')
    <h1>Login</h1>

    @include('_partials.errors')

    {{ Form::open(array('route' => 'register', 'method' => 'POST')) }}

    {{ Form::token() }}

    <p>
        {{ Form::label('username', 'Username') }}
        {{ Form::text('username', Input::old('username')) }}
    </p>

    <p>
        {{ Form::label('password', 'Password') }}
        {{ Form::text('password') }}
    </p>

    <p>
        {{ Form::submit('Login') }}
    </p>

    {{ Form::close()}}

@stop

Upvotes: 0

Views: 850

Answers (3)

JohnD
JohnD

Reputation: 76

I don't know what concepts are new to you , so let me make a couple arbitrary assumptions . If "namespace" and "Basecontroller" are << strange words >> , let me try to demystify these words .

Namespace : PHP's documentation is pretty well documented on this subject . My oversimplified explanation is as follows : Two skilled developers (JohnD and Irish1) decide to build their own PHP Logging Library and release the code as open source to the community .Most likely they will name their library "Log"
Now another developer would like to implement both libraries into his/her project (because JohnD's code uses MongoDB as storage medium while Irish1's code uses Redis ) . How would PHP's interpreter distinguishes the two code-bases from each other ? Simply prepend each library with a vendor name (JhonD/Log and Irish1/Log ) .

Basecontroller : Most likely your Controllers will share common functionality (a database connection , common before/after filters , a common template for a View ...... ) . It is a good practice not to define this "common functionality" into each Controller separately, but define a "Parent" Controller , from which all other Controllers will inherit its functionality . So later on , if you decide to make changes on the code , only one place should be edited . My previous example uses " class RegisterController extends BaseController " , that BaseController is just checking if our (or any other) Child-controller has defined a property with the name of " $layout " , and if so , the View that it will instantiate will be encapsulated into that specified layout . See Laravel's flexibility , a group of Controllers share common functionality (by extending Basecontroller) but also are free to choose their own layout (if they desire to do so ) .

Upvotes: 1

JohnD
JohnD

Reputation: 76

You could also define the layout template directly from the Controller , this approach provides more flexibility , as the same View can be used with multiple layout templates .

<?php namespace App\Controllers ;

use View , BaseController  ; 

class RegisterController extends BaseController {

    protected $layout = 'layouts.master';

    public function getIndex()
    {
        // Do your stuff here 

        // --------- -------

        // Now call the view 
        $this->layout->content = View::make('registration-form');
    }

}

My example uses Namespaced Controller but the same concepts are applicable on non-Namespaced Controllers .

Notice : Our RegisterController extends Laravel's default BaseController , which makes a bit of preparation for us , see code below :

<?php

class BaseController extends Controller {

    /**
     * Setup the layout used by the controller.
     *
     * @return void
     */
    protected function setupLayout()
    {
        if ( ! is_null($this->layout))
        {
            $this->layout = View::make($this->layout);
        }
    }

}

If a custom "Basecontroller" is defined , make sure that it also implements the "preparation" code .

Upvotes: 1

Mark
Mark

Reputation: 3197

I have found my error

I did not have @extends('layouts.default') at the beginning of the login.blade.php template

Upvotes: 0

Related Questions