简体   繁体   English

如何在Laravel 4.2中将字符串回显到视图中?

[英]How to echo a string into a view in Laravel 4.2?

I am using Laravel 4.2 framework (will be upgraded soon) and am calling an artisan command from a controller with a return redirect back. 我正在使用Laravel 4.2框架(即将升级),并且正在从控制器返回返回重定向回调用artisan命令。

My issue is when I call the command, the command echo some stuff for me (which is helpful when I call it from the terminal) but now since am using it in a method, I want the same echos showing in my view as flash message. 我的问题是当我调用该命令时,该命令为我echo一些东西(当我从终端调用它时很有帮助),但是现在由于在一种方法中使用它,我希望在视图中以闪现消息的形式显示相同的echos

I will write the following example just to clarify my issue. 我将编写以下示例来澄清我的问题。

My Command: 我的命令:

public function fire()
{ 
    // Do stuff.
    echo $vars 
}

My Controller: 我的控制器:

function foo() {

      // Some input here

      Artisan::call('command');


      return Redirect::back(); // <-- want to add the echos of the command. 
}

So I want to show the $var in my views, am not sure if it's possible if not am open to other suggestions that can give me the same concept (keep in mind if am replacing the echo with something else it got to show in both view and terminal when I run the command). 因此,我想在我的视图中显示$var ,不确定是否可以接受其他可以给我相同概念的建议(请注意,如果要用两个都显示的其他内容代替echo ,运行命令时查看和终端)。

Note: I tried Ajax but my issue was with me sending a file as param, tried the FormData Object but it didn't work with me. 注意:我尝试了Ajax,但是我的问题是我发送了一个文件作为参数,尝试了FormData对象,但是它对我不起作用。

Storing data in the session is a good way of keeping persistent data throughout multiple requests. 在会话中存储数据是在多个请求中保留持久性数据的一种好方法。 Session flashing is a way to keep data just for the next request, which is perfect for redirects. 会话刷新是一种只为下一个请求保留数据的方法,非常适合重定向。 Laravel uses this internally for old input data when there is a validation error. 发生验证错误时,Laravel在内部将其用于旧输入数据。

https://laravel.com/docs/4.2/session#flash-data https://laravel.com/docs/4.2/session#flash-data

Session::flash('key', 'value');

This will only be kept for the next request, in which you'd be able to use Session::get('key') to retrieve it. 这只会保留给下一个请求,您可以在其中使用Session::get('key')检索该请求。

I fixed the problem by changing my command little bit. 我通过稍微更改命令来解决了这个问题。

Command: 命令:

function fire()
{
  // replaced the echo with the following.
  $this->info('what ever you want ur message you want to put in console');

 }

Controller 控制者

function foo()
{

  $output =  new BufferedOutput();

  Artisan::call('command', [arguments], $output);

  return Redirect::back()->with('flash_message', $output->fetch());
}

Guess the buffered output works with command outputs which can be found in Laravel Documentations not echo's 猜猜缓冲的输出是否与命令输出一起使用,可以在Laravel文档中找到它们, 不是echo's

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

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