Reputation: 5355
I am a newbie in laravel 4. When pressing a button I want to display a message on the screen. This is my view:
@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
My routes are defined like that:
Route::get('pressMe', 'TestController@getIndex');
Route::post('pressMe', 'TestController@postTest');
Route::controller('pressMe', 'TestController');
Finally this is my test controller:
<?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);
}
}
However, when pressing the button nothing gets shown on the screen.
Any recommendations what I am doing wrong?
I really appreciate your answer!
UPDATE
In my filters file I have a protection against CSS, however, I now get the following error:
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
That´s my filter:
Route::filter('csrf', function()
{
if (Session::getToken() != Input::get('csrf_token') && Session::getToken() != Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});
What am I doing wrong?
Upvotes: 0
Views: 58
Reputation: 6393
you have lots of half completed codes in your snippet.
view:
you have a submit button yet you have no form in place. either use ajax, or javascript to submit the form or simpler, just write the form.
@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
remove the
@if(isset($pressMe))
{{$pressMe }}
@endif
and add this instead,
@if(Session::has('pressMe'))
{{Session::get('pressMe')}}
@endif
Just like i did above in the view code snippet
In the route file:
Route::get('pressMe', 'TestController@getIndex');
Route::post('pressMe', 'TestController@postTest');
remove Route::controller('pressMe', 'TestController');
Don't mix up manual routing and controller routing. It will give you headache later. Stick to one. Either Controller routing, or Manual Routing, or Restful routing. don't mix!
In the Test Controller:
public function postTest() {
$pressMe = 'It works!';
return Redirect::to('/pressMe')->with('pressMe', $pressMe);
}
.... and you are done.
view:
<form method="post" action="{{URL::action('TestController@postTest')}}">
and
TestController:
return Redirect::action('TestController@postTest')->with('pressMe', $pressMe);
Upvotes: 1