将php代码插入flash消息中

时间:2014-12-06 15:57:32

标签: php laravel laravel-4

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

<?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放入路由或控制器中。但我不知道该怎么做,我没有声明正确的变量。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

你在这里混合了一些编码风格,你的控制器中有一些你的观点元素让人感到困惑。

尝试将要显示的变量传递到刀片视图,然后使用{{ $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

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