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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵