简体   繁体   English

laravel 5.2使用消息重定向

[英]laravel 5.2 redirect with message

if user of site visits /page, I want him to redirect to index with message. 如果站点的用户访问/ page,我希望他重定向到带有消息的索引。 How do I access redirect message in view? 如何在视图中访问重定向消息? My routes: 我的路线:

Route::group(['middleware' => 'web'],function(){ 
     Route::get('/page', function () {
         return redirect('/')->withMessage(["warning"=> ["yeah","test"]]); // According official docs, this should work.
     }); 
     Route::get('/', 'Page@front');
});

My Page controller: 我的页面控制器:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\News;
use App\PageContact;
use Session;

class Page extends Controller{
    public function front(Request $Request){
        return view('index')->withNews("news");
}

important: yes, my pages are already wrapped in web middleware. 重要:是的,我的页面已经包装在Web中间件中。 And please avoid posting Laravel 3 or Laravel 4 solutions. 并且请避免发布Laravel 3或Laravel 4解决方案。

It should be: 它应该是:

->with('warning', 'testing')

Then in the view: 然后在视图中:

@if(session()->has('warning'))
    {!! session()->get('warning') !!}
@endif

The answer from user2094178 is correct, but you don't have to use 来自user2094178的答案是正确的,但是您不必使用

->with('warning', 'testing')

it can also be (as you did): 也可以是(如您所做的那样):

->withNews("news");
->withWarning('testing');

Since the View-class is using the magic function "__call": 由于视图类使用魔术函数“ __call”:

     /**
     * Dynamically bind parameters to the view.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return \Illuminate\View\View
     *
     * @throws \BadMethodCallException
     */
    public function __call($method, $parameters)
    {
        if (Str::startsWith($method, 'with')) {
            return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
        }

        throw new BadMethodCallException("Method [$method] does not exist on view.");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM