0

我是 laravel 的新手,并遵循基本应用程序的教程。到目前为止,该应用程序有一个默认视图 layouts/default.blade.php、一个部分 _partials/errors.blade.php 和其他三个视图 questions/index.blade.php、users/new.blade.php 和 users/login.blade .php

路线是这样定义的

// 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 和 users/new.blade.php 加载正常且在 default.blade.php 内

当我调用 /login 时,即使使用 default.blade.php,也会加载一个空白页面。我猜我的刀片语法在 login.blade.php 中存在问题,因为 default.blade.php 可以在其他路由上工作,据我所知,其他一切都是一样的,但如果是这样的话案例不会至少加载 default.blade.php 路由吗?

该路由调用的控制器方法如下

<?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();
            }
        }
    }
?>

最后 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
4

3 回答 3

1

您也可以直接从 Controller 定义布局模板,这种方法提供了更大的灵活性,因为同一个 View 可以与多个布局模板一起使用。

<?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');
    }

}

我的示例使用命名空间控制器,但相同的概念适用于非命名空间控制器。

注意:我们的 RegisterController 扩展了 Laravel 的默认 BaseController ,这为我们做了一些准备,见下面的代码:

<?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);
        }
    }

}

如果定义了自定义“Basecontroller”,请确保它也实现了“准备”代码。

于 2013-09-11T22:26:51.647 回答
1

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 ) .

于 2013-09-12T08:44:13.853 回答
0

我发现了我的错误

我在 login.blade.php 模板的开头没有 @extends('layouts.default')

于 2013-09-11T19:21:31.137 回答