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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c