在闪存消息中插入 php 代码


Insert php code into a flash message

我想将此代码插入闪存消息中,但似乎闪存消息不接受闪存消息

<?php echo Session::get('notify') ? "<p style='color:green'>" . Session::get('notify') . "</p>" : "" ?>
<h1>Welcome <?php echo $user->username ?></h1>
<p>Your email: <?php echo $user->email ?></p>
<p>Your account was created on: <?php echo $user->created_at ?></p>
?>

Route::post('registration', array('before' => 'csrf',function()
{
    $rules = array(
        'username' => 'required|unique:users',
        'email'    => 'required|email|unique:users',
        'password' => 'required|min:8|same:password_confirm'
    );
    $validation = Validator::make(Input::all(), $rules);
    if ($validation->fails())
    {
        return Redirect::to('registration')->withErrors($validation)->withInput();
    }
    $user           = new User;
    $user->username = Input::get('username');
    $user->email    = Input::get('email');
    $user->password = Hash::make(Input::get('password'));
    if ($user->save())
    {
        Auth::loginUsingId($user->id);
        return Redirect::to('index')->with('flash_message', 'Thank you for registering. {{ echo Session::get('notify') ? "<p style='color:green'>" . Session::get('notify') . "</p>" : "" ?>
            <h1>Welcome <?php echo $user->username ?></h1>
            <p>Your email: <?php echo $user->email ?></p>
            <p>Your account was created on: <?php echo $user->created_at ?></p>
            <p><a href="<?= URL::to('profile-edit') ?>">Edit your information</a></p>}}'
        );
    }
    return Redirect::to('registration')->withInput();
}));

我该怎么做?

我尝试重命名flash_message并将其放在 master.blade 中.php如下所示:

@if(Session::get('login_message'))
    <div class='login-message'>{{ Session::get('login_message') }}
        <?php echo Session::get('notify') ? "<p style='color:green'>" . Session::get('notify') . "</p>" : "" ?>
        <h1>Welcome <?php echo $user->username ?></h1>
        <p>Your email: <?php echo $user->email ?></p>
        <p>Your account was created on: <?php echo $user->created_at ?></p>
        <p><a href="<?= URL::to('profile-edit') ?>">Edit your information</a></p> ?>
    </div>
@endif

但它没有用,因为我想我将不得不将 php 放入路由或控制器中。但是我不知道该怎么做,我未能声明正确的变量。有人可以帮助我吗?

你在这里混合了一些编码风格,你的控制器中有一些视图的元素,这令人困惑。

尝试将要显示的变量传递到边栏选项卡视图,然后使用 {{ $variable }} 表示法包含它们:

在您的操作中,将用户传递回视图:

// (snip) 
if ($user->save())
{
    Auth::loginUsingId($user->id);
    return Redirect::to('index')
                   ->with('login_message', 'Thank you for registering.') 
                   ->with('user', $user);
}
// (snip) 

并在视图中提取用户的这些元素(现在都在Session中(:

@if(Session::has('login_message'))
    <div class='login-message'>
        {{ Session::get('login_message') }} 
        <h1>Welcome {{ Session::get('user')->username }}</h1>
        <p>Your email: {{ Session::get('user')->email }}</p>
        <p>Your account was created on: {{ Session::get('user')->created_at }}</p>
        <p><a href="{{ URL::to('profile-edit') }}">Edit your information</a></p> ?>
    </div>
@endif

这应该可以帮助您朝着正确的方向前进。