I want redirect guest on login with message, but method:
Auth::user();
redirect on page login only without message. How I can add message?
Example:
redirect()->route('login')->with('error', 'Please auth!!');
Advertisement
Answer
You can do it using a middleware, that performs a redirect. Create the middleware at app/Http/Middleware/Withmessage.php and add it to app/Http/Kernel.php after StartSession string.
protected $middlewareGroups = [
'web' => [
...
IlluminateSessionMiddlewareStartSession::class,
AppHttpMiddlewareWithmessage::class, // <-- here
IlluminateViewMiddlewareShareErrorsFromSession::class,
...
],
...
];
The code of a middleware contains only one string, that differs from a clean middleware.
namespace AppHttpMiddleware;
use Closure;
use Auth;
class Withmessage {
public function handle($request, Closure $next)
{
$route = $request->route()->getName();
if(!Auth::user() and $route === 'button-click-button') {
return redirect()->route('login')->with('error', 'Please auth!!');
}
return $next($request);
}
}