Php Laravel:将用户重定向到另一个包含消息的页面

Php Laravel:将用户重定向到另一个包含消息的页面,php,laravel,Php,Laravel,以下是用于添加新用户的控制器代码摘录: public function store() { $input = Input::all(); if (! $this->user->isValid($input)) { return Redirect::back()->withInput()->withErrors($this->user->errors); } ... } 此处显示“添加新用户”窗体的控

以下是用于添加新用户的控制器代码摘录:

public function store()
{
    $input = Input::all();

    if (! $this->user->isValid($input))
    {
        return Redirect::back()->withInput()->withErrors($this->user->errors);
    }

    ...
}
此处显示“添加新用户”窗体的控制器代码:

public function create()
{
    return View::make('users.create');
}
请注意,我不需要向视图发送输入和错误,但我可以在那里访问它,而不会出现任何问题

但请看一些其他代码:

以下是用于删除用户的控制器代码:

public function destroy($id)
{
    $user = User::find($id);

    $deleted_message = 'User "' . $user->first_name . ' ' . $user->last_name . '" has been deleted.';

    User::destroy($id);

    return Redirect::route('users.index')->withMessage($deleted_message);
}
以下是显示所有用户的控制器代码:

public function index()
{
    $users = User::all();
    return View::make('users.index')->withUsers($users);
}
为什么视图中没有显示所有用户的
$message

为什么视图中没有显示所有用户的$message

因为您没有检索它。使用
withX()
magic方法将数据放入闪存。也就是说,你需要从那里取回它

<?php 

class UserController extends Controller {

    public function index()
    {
        $message = Session::get('message');

        $users = [];

        return Redirect::make('users.index')->withUsers($users)->withMessage($message); 
    }

    public function destroy()
    {
        $deleted_message = "Some message that shows that something was deleted";

        return Redirect::route('users.index')->withMessage($deleted_message);
    }
    
}

您可以通过以下方式发送信息:

return Redirect::route('your-route')->with('global', 'Your message');
@if(Session::has('global'))
<p>{{ Session::get('global') }}</p>
@endif
并使用以下内容将其放入模板中:

return Redirect::route('your-route')->with('global', 'Your message');
@if(Session::has('global'))
<p>{{ Session::get('global') }}</p>
@endif
@if(Session::has('global'))
{{Session::get('global')}

@恩迪夫
您没有在索引代码中发送
$消息。为什么会这样?@fmgonzalez我甚至没有在创建代码时发送
$input
$errors
。但是我可以访问这两个变量。withInput()和withErrors()是保留的方法。另外:
$errors
变量可通过laravel在全球范围内使用。谢谢@ThomasDavidPlat。这就是我一直在寻找的答案。但是我如何在不做任何事情的情况下访问创建视图代码中的
$input
$errors
?正如您在评论中指出的那样,
$errors
$input
自动提供给每个视图。因此,我们不需要将它们发送到视图。但是我们需要发送其他消息来查看。由于您已经添加了如何从会话中获取消息并将其发送到视图的代码,因此我接受将其作为解决方案。是的,这也会起作用(不过我没有测试它)。但我认为最好(从表示角度)从控制器中的会话获取消息,并将其作为变量发送到视图。