php 如何使用 Laravel 重定向发送数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25078452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:38:20  来源:igfitidea点击:

How to send data using redirect with Laravel

phplaravellaravel-4blade

提问by Anastasie Laurent

I developed an API to show a pop up message when the page loaded.

我开发了一个 API 来在页面加载时显示弹出消息。

Not all the pages use the pop up API. For example, if the user go to the show($id)page, that doesn't required the pop up api to fire. but in some special cases, I need the pop up to be fired.

并非所有页面都使用弹出 API。例如,如果用户转到该show($id)页面,则不需要触发弹出 api。但在某些特殊情况下,我需要弹出弹出窗口。

Here is my code, ** this code is just to illustrate my point, not an actual working code**

这是我的代码,** 这段代码只是为了说明我的观点,而不是实际的工作代码**

public function store(){
    validating the input

    saving them

    $id = get the id

    return Redirect::route('clients.show, $id')
}

and in the show function I do this:

在 show 函数中,我这样做:

public function show($id){
   $client =Client::find($id)
   return View::make('clients.profife')->with(array(
   'data' => $client
))

My question

我的问题

Is there a way so I can send a data from the storefunction to the showfunction using Redirect::route? and then in the showfunction, I check if this showfunction, I check if this data has been sent of what and then I decide whether to fire the pop up api or not.

有没有办法让我可以使用 Redirect::route将数据从store函数发送到show函数?然后在函数中,我检查这个函数,我检查这个数据是否已经发送,然后我决定是否触发弹出的api。showshow

}

}

回答by peter.babic

In store()

有存货()

return Redirect::route('clients.show, $id')->with( ['data' => $data] );

and in show()read it with

并在show()阅读它

Session::get('data');

回答by vozaldi

A simple redirect using helper functions.

使用辅助函数的简单重定向。

So you don't need to set use Redirectnor use Sessionin your Controller.

所以你不需要在你的控制器中设置use Redirectuse Session

After you're done processing something in your Controller, insert:

在 Controller 中完成处理后,插入:

return redirect()->route( 'clients.show' )->with( [ 'id' => $id ] );

To retrieve the variable 'id'in route 'clients.show', use:

要检索'id'route 中的变量'clients.show',请使用:

// in PHP
$id = session()->get( 'id' );

// in Blade
{{ session()->get( 'id' ) }}

回答by Sikander

Yes you can define path in route like below:

是的,您可以在路由中定义路径,如下所示:

Route::get('store/{id?}/{param1?}/{param2?}/{param3?}', 'clients@store');

You can pass multiple parameter in function and read in controller by passing argument..

您可以在函数中传递多个参数并通过传递参数在控制器中读取..

You can define in controller :

您可以在控制器中定义:

public function store(id, param1,param2,param3){
//read the param values
}