在 .blade.php 模板中使用 require_once 是一种好习惯吗?

Is it a good practice to use require_once in the .blade.php template?

通常我们会使用类似

的东西
<?php
require_once 'init.php'; //file for start the session, connect to database etc.
?>
<!-- HTML Content Here-->
head body etc.
<!---------------------->

但是当您构建 .blade.php 模板时。事情也是这样吗? 我的意思是我的模板类似于

<?php
require_once 'init.php';
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>@yield('title')</title>
    <link rel="stylesheet" href="../public/css/bootstrap.min.css" media="screen">
    <link rel="stylesheet" href="../public/css/tpl.css">
    <link rel="stylesheet" href="../public/fonts/font.css">
</head>
<body>
    @yield('header')

    @yield('content')

    @yield('footer')
</body>
</html>

这是最佳做法还是有更好的方法?

您可以使用

include"init.php";

include_once"init.php";

引用http://www.w3schools.com/php/php_includes.asp

我建议您学习不需要这些步骤的 Laravel 身份验证

Laravel已经为您处理了这些工作。

这是Laravel Authentication Document

如果您使用的是Laravel 4.2,那么

在你的路线中,你应该添加

Route::group(array('before' => 'auth'), function(){ #Your Request here});

这是您

的示例路线
Route::get('home', 'YourControllerr@YourGeneralFunction');
Route::group(array('before' => 'auth'), function()
{
Route::get('dashboard', 'YourControllerr@YourSecureFunction');
});

在上述路线中,任何人 (Public) 都可以访问 url home。但是 url dashboard 只能由登录用户访问。

如果你用的是Laravel 5以上的,那就更简单了

您可以通过

简单地检查用户

如果他们的用户是访客,那么

@if(Auth::guest())

或者如果用户已通过身份验证则

if (Auth::check())
{
    // The user is logged in...
}

@if( Auth::check() )
    Current user: {{ Auth::user()->name }}
@endif