duanjing9339 2015-05-15 03:47
浏览 151
已采纳

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

Normally we'll use something like

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

But when you're building a .blade.php template. Is thing also work that way? I mean my template is something like

<?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>

Is this a best practice or is there a better way to do this?

  • 写回答

2条回答 默认 最新

  • douhuang4166 2015-05-15 08:08
    关注

    I would recommend you to Study the Laravel Authentication which doesn't require such steps.

    Laravel already handles such works for you.

    Here is the Laravel Authentication Document

    If you are using Laravel 4.2, then

    In your routes, you should add the

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

    Here is the sample route for your

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

    In the Above routes the url home can be accessed by anyone (Public). But the url dashboard can be only accessed by the logged in user.

    If you are using the Laravel 5 or above, then it is even simple

    You can simply check the user by

    If they user is guest then

    @if(Auth::guest())
    

    Or if the user is authenticated then

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

    or

    @if( Auth::check() )
        Current user: {{ Auth::user()->name }}
    @endif
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?