guyaloni
guyaloni

Reputation: 5902

Laravel nova - redirect from Dashboard

I would like to remove dashboard from my Laravel Nova app.

I found it easy to remove it from sidebar-menu - simply comment /views/dashboard/navigation.blade.php code.

However, I want to add a redirection logic (landing page depends on user role) so when navigating to / user will be redirected to a resource or tool which corresponds him.

(I have already implemented a redirection after login (https://stackoverflow.com/a/54345123/1039488)

I tried to do it with cards, but looks like this is not the right solution.

Any idea where can I place the redirection logic?

Upvotes: 10

Views: 8488

Answers (5)

Imran Zahoor
Imran Zahoor

Reputation: 2787

A cleaner and safe way for Nova 3.x or below:

  • Copy vendor/laravel/nova/resources/views/layout.blade.php to resources/views/vendor/nova/
  • Now open resources/views/vendor/nova/layout.blade.php and edit it
  • Replace this line with the code below window.Nova = new CreateNova(config);
    window.Nova = new CreateNova(config);
    window.Nova.booting((Vue, router, store) => {
        /** This fixes showing an empty dashboard. */
        router.beforeEach((to, from, next) => {
            if (to.name === 'dashboard.custom') {
                next({ name: 'index', params: { resourceName: 'users'}});
            }

            next();
        });
    });
  • Replace users with your entity name's plural like products
  • Now save the file and refresh the nova dashboard and you should see the new page.

The solution was taken from here with clear steps.

The solution may also work for 4.x, but I haven't checked it yet.

Happy Coding :)

Upvotes: 1

Pre nova 4 method:

To NovaServiceProvider.php add to boot method:

Nova::script('menuFix', __DIR__.'/../../resources/js/fixMenu.js');

Create file fixMenu.js with following:

if (location.pathname == '/' || location.pathname == '/dashboards/main'){
    location.href = '/whereToRedirect'
}

Upvotes: 3

Ron van der Heijden
Ron van der Heijden

Reputation: 15080

Nova 4; You can override the initialPath like so:

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();

        Nova::initialPath('/resources/users');
    }

    // ...
}

This way, you get redirected to the Users resource upon logging in.

Upvotes: 10

Jerry Chee
Jerry Chee

Reputation: 21

i came across this link : Laravel Nova - Point Nova path to resource page

Not sure it's a permanent solution but editing LoginController.php will do.

public function redirectPath()
{
    return Nova::path().'/resources/***<resource_name>***;
}

**change to your own resource name

Upvotes: 0

lwitzel
lwitzel

Reputation: 591

Just figured this out myself. In your Routes/web.php file, add a redirect route:

Route::redirect('/','/resources/{resource_name}');

where {resource_name} is the plural form of the resource. For example, '/resources/posts'.

In your case, you may want to redirect to your own control file, where the redirect logic can be placed.

Route::get('/', 'YourController@rootRedirectLogic');

Then in the controller YourController, add the method:

public function rootRedirectLogic(Request $request) {
    // some logic here
    return redirect()->route('YourRoute');
}

where 'YourRoute' is the name of the route you want to send the user to.

(Found clues to this solution in a comment by dillingham here: https://github.com/laravel/nova-issues/issues/393)

Upvotes: 0

Related Questions