Skip to content

Commit

Permalink
Session is no longer available for service providers. Create a middle…
Browse files Browse the repository at this point in the history
…ware
  • Loading branch information
ghunti committed Apr 24, 2015
1 parent 73cb438 commit fe93111
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ If you want to contribute don't hesitate.
}
```

Add the following middleware to the app/Http/Middleware:
```php
protected $middleware = [
...<other middleware>...,
'Ghunti\LaravelBase\Middleware\ShareMessagesFromSession',
];
```

## Features

### Permission Denied Exception
Expand Down
41 changes: 41 additions & 0 deletions src/Middleware/ShareMessagesFromSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Ghunti\LaravelBase\Middleware;

use Closure;
use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Support\MessageBag;
use Illuminate\Contracts\View\Factory as ViewFactory;

class ShareMessagesFromSession implements Middleware
{
/* Create a new error binder instance.
*
* @param \Illuminate\Contracts\View\Factory $view
* @return void
*/
public function __construct(ViewFactory $view)
{
$this->view = $view;
}

/**
* Same logic provided by Illuminate\View\Middleware::ShareErrorsFromSession to bind
* the messages from session to the view.
* If 'messages' exist in the session, a variable $messages will be inserted into the view.
* If no 'messages' exist on the session an empty MessageBag is inserted
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->session()->has('messages')) {
$this->view->share('messages', $request->session()->get('messages'));
} else {
$this->view->share('messages', new MessageBag);
}
return $next($request);
}
}
19 changes: 0 additions & 19 deletions src/ServiceProviders/LaravelBaseServiceProvider.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Ghunti\LaravelBase\ServiceProviders;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\MessageBag;
use Ghunti\LaravelBase\Validation\Validator;
use Ghunti\LaravelBase\Routing\Redirector;
use Session;
Expand Down Expand Up @@ -51,9 +50,6 @@ function ($translator, $data, $rules, $messages) {
return new Validator($translator, $data, $rules, $messages);
}
);

//Bind messages from session to the view
$this->registerMessagesWithView();
}

/**
Expand All @@ -65,19 +61,4 @@ public function provides()
{
return array();
}

/**
* Same logic provided by ViewServiceProvider::registerSessionBinder to bind
* the messages from session to the view.
* If 'messages' exist in the session, a variable $messages will be inserted into the view.
* If no 'messages' exist on the session an empty MessageBag is inserted
*/
protected function registerMessagesWithView()
{
if ($messageBag = Session::get('messages')) {
$this->app->view->share('messages', $messageBag);
} else {
$this->app->view->share('messages', new MessageBag);
}
}
}

0 comments on commit fe93111

Please sign in to comment.