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 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题