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 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏