Question: On my app, I'm trying to make it so that if a user has a certain condition, he will ALWAYS be redirected to a certain page, no matter which route he tries to access. In this case, it's if he doesn't have a username (long story).

**ComposerServiceProvider.php** :

```
public function boot() {
    View::composer('templates.default', function ($view) {

        if(Auth::user()) {

            if (Auth::user()->username == null || Auth::user()->username == "") {
                return redirect()->route('auth.chooseUsername');        
            }

```

So I figured the place to do this would be 
`ComposerServiceProvider.php`. 

However, I'm noticing that my redirect don't work in `ComposerServiceProvider.php`. And `laravel.log` doesn't give me an error or reason why. 

The if condition is being met. If I replace `return redirect()->route('auth.chooseUsername');` with `dd('test');`, sure enough all my pages return 'test'.

Why is this happening?

Answer: 
Try this steps:

You can use middleware for this scenario like below:

1. Create middleware `php artisan make:middleware CheckPoint`

Inside `App\Http\Middleware\CheckPoint.php` File 

```
    use Closure;

    class CheckPoint
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
            if($request->user()) {
                if ($request->user()->username == null || $request->user()->username == "") {
                    return redirect()->route('auth.chooseUsername');        
                }
            }
            return $next($request);
        }
    }

```

2. Add the middleware inside the `app\Http\kernel.php`

```
    protected $routeMiddleware = [
        'checkPoint' => \App\Http\Middleware\CheckPoint::class,
    ];

```

3. Then you can use it inside your route file and controller like below

`Route::get(...)->middleware('checkPoint');`

`Route::middleware('checkPoint')->group(function() {
 //Group of routes
 .....
});`

[More About Middleware](https://laravel.com/docs/5.5/middleware)

[controller middleware](https://laravel.com/docs/5.5/controllers#controller-middleware)
In `App\Http\Middleware` **create a new middleware**:

```
<?php

namespace App\Http\Middleware;

use Closure;

class CheckYourCondition
{
    public function handle($request, Closure $next)
    {
        if (! $request->user()->yourCondition()) {
            return redirect('your_target_routre');
        }

        return $next($request);
    }
}

```

Register your middleware by adding it to protected `$routeMiddleware` in `App\Http\Kernel`.

and assing it to the 'web' middleware group in `protected $middlewareGroups`.

For details [see](https://laravel.com/docs/5.5/middleware)

**The ComposerServiceProvider** has a different purpose. It is used to register View Composers.

> 
> View composers are callbacks or class methods that are called when a
>  view is rendered. If you have data that you want to be bound to a view
>  each time that view is rendered, a view composer can help you organize
>  that logic into a single location.
> 
> 
> 

[See View Composers](https://laravel.com/docs/5.5/views#view-composers).