按下按钮时显示控制器的var

时间:2014-11-12 06:42:45

标签: php laravel laravel-4

我是laravel 4的新手。当按下按钮时,我想在屏幕上显示一条消息。 这是我的观点:

@section('content')
    <div class="page-header">
        <h3>
            Management
        <div class="pull-right">
                    {{ Form::submit('Save', array('class' => 'btn btn-small btn-info iframe')) }}
                @if(isset($pressMe))
                    {{$pressMe }}
                @endif
            </div>
        </h3>
    </div>
@stop

我的路线定义如下:

Route::get('pressMe', 'TestController@getIndex');
Route::post('pressMe', 'TestController@postTest');
Route::controller('pressMe', 'TestController');

最后这是我的测试控制器:

<?php
class TestController extends BaseController {

/**
 * Inject the models.
 */
public function __construct() {
    parent::__construct ();
}

public function getIndex() {

    // Show the page
    return View::make ( 'admin/pressMe/index' )->with('pressMe', '');;
}

/**
 * Start Test script.
 */
public function postTest() {
    $pressMe = 'It works!';

    return View::make ( 'admin/pressMe/index' )->with('pressMe', $pressMe);
}
}

然而,当按下按钮时,屏幕上不会显示任何内容。

任何建议我做错了什么?

我非常感谢你的回答!

更新

在我的过滤器文件中,我有一个针对CSS的保护,但是,我现在收到以下错误:

Illuminate\Session\TokenMismatchException thrown with message ""

Stacktrace:
#21 Illuminate\Session\TokenMismatchException in C:\xampp\htdocs\laravel\lara\app\filters.php:96
#20 {closure} in <#unknown>:0
#19 call_user_func_array in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php:199
#18 Illuminate\Events\Dispatcher:fire in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Events\Dispatcher.php:154
#17 Illuminate\Events\Dispatcher:until in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Routing\Router.php:1468
#16 Illuminate\Routing\Router:callRouteFilter in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:240
#15 Illuminate\Routing\ControllerDispatcher:callFilter in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:114
#14 Illuminate\Routing\ControllerDispatcher:before in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:55
#13 Illuminate\Routing\ControllerDispatcher:dispatch in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Routing\Router.php:962
#12 Illuminate\Routing\Router:Illuminate\Routing\{closure} in <#unknown>:0
#11 call_user_func_array in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Routing\Route.php:109
#10 Illuminate\Routing\Route:run in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Routing\Router.php:1028
#9 Illuminate\Routing\Router:dispatchToRoute in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Routing\Router.php:996
#8 Illuminate\Routing\Router:dispatch in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:775
#7 Illuminate\Foundation\Application:dispatch in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:745
#6 Illuminate\Foundation\Application:handle in C:\xampp\htdocs\laravel\lara\vendor\barryvdh\laravel-debugbar\src\Middleware\Stack.php:34
#5 Barryvdh\Debugbar\Middleware\Stack:handle in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Session\Middleware.php:72
#4 Illuminate\Session\Middleware:handle in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Cookie\Queue.php:47
#3 Illuminate\Cookie\Queue:handle in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Cookie\Guard.php:51
#2 Illuminate\Cookie\Guard:handle in C:\xampp\htdocs\laravel\lara\vendor\stack\builder\src\Stack\StackedHttpKernel.php:23
#1 Stack\StackedHttpKernel:handle in C:\xampp\htdocs\laravel\lara\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:641
#0 Illuminate\Foundation\Application:run in C:\xampp\htdocs\laravel\lara\public\index.php:49

这是我的过滤器:

Route::filter('csrf', function()
{
    if (Session::getToken() != Input::get('csrf_token') &&  Session::getToken() != Input::get('_token'))
    {
        throw new Illuminate\Session\TokenMismatchException;
    }
});

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的代码段中包含大量已完成的代码。

视图

你有一个提交按钮,但你没有表格。使用ajax或javascript提交表单或更简单,只需编写表单。

@section('content')
    <div class="page-header">
        <h3>
            Management
        <div class="pull-right">
             <form method="post" action="/pressMe">
               <input type="submit" class="btn btn-small btn-info iframe" value="Save" />
             </form>

             @if(Session::has('pressMe'))
               {{Session::get('pressMe')}}
             @endif 

            </div>
        </h3>
    </div>
@stop

删除

 @if(isset($pressMe))
   {{$pressMe }}
 @endif

并添加此内容,

@if(Session::has('pressMe'))
    {{Session::get('pressMe')}}
@endif

就像我在视图代码段中所做的那样

在路线档案中:

Route::get('pressMe', 'TestController@getIndex');
Route::post('pressMe', 'TestController@postTest');

删除Route::controller('pressMe', 'TestController');

不要混淆手动路由和控制器路由。它会让你头疼。坚持一个。控制器路由,手动路由或Restful路由。 不要混用!

在测试控制器中

public function postTest() {
    $pressMe = 'It works!';

    return Redirect::to('/pressMe')->with('pressMe', $pressMe);
}

.... 你已经完成了。

更新#1

查看:

<form method="post" action="{{URL::action('TestController@postTest')}}">

<强>的TestController:

return Redirect::action('TestController@postTest')->with('pressMe', $pressMe);