enadabuzaid's avatar

create reusable Dashboard

I want to create a reusable dashboard and have it on a separate repository what is the best practice to use this dashboard in many projects

  • Dashboard on repo

  • the project on repo

both work in Laravel so how can use this dashboard in multiple projects working on larvae!?

0 likes
4 replies
LaryAI's avatar
Level 58

Creating a reusable dashboard for multiple Laravel projects is a great idea to maintain consistency and reduce duplication of effort. Here's a step-by-step guide on how to achieve this:

  1. Create a Laravel Package: The best practice for creating a reusable piece of functionality in Laravel is to create a package. A package is like any other Laravel project but is designed to be integrated into other Laravel applications.

  2. Develop the Dashboard: Develop your dashboard within this package. Make sure to use views, routes, controllers, and any other necessary components that your dashboard requires.

  3. Use Service Providers: Utilize a service provider to bind your package's functionality into the Laravel app. This is where you'll load routes, views, and publish assets.

  4. Configuration and Assets: If your dashboard requires configuration, use configuration files that can be published to the host application. Similarly, publish assets like CSS, JavaScript, and images that the dashboard requires.

  5. Testing: Test your package thoroughly to ensure it works independently of your application.

  6. Version Control: Push your package to a version control repository, such as GitHub, GitLab, or Bitbucket.

  7. Packagist Submission: If you want to make it easy to install, submit your package to Packagist, which is the default package repository for Composer.

  8. Documentation: Write clear documentation on how to install and use your dashboard package in other Laravel projects.

  9. Integration: In your other Laravel projects, require the dashboard package via Composer:

    composer require vendor/package-name
    
  10. Publish Assets and Configurations: After requiring the package, publish its assets and configurations to your project:

    php artisan vendor:publish --provider="Vendor\PackageName\PackageNameServiceProvider"
    
  11. Use the Dashboard: Finally, use the dashboard as intended in your Laravel projects.

Here's a basic example of what the service provider in your package might look like:

namespace Vendor\PackageName;

use Illuminate\Support\ServiceProvider;

class PackageNameServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Load routes
        $this->loadRoutesFrom(__DIR__.'/routes/web.php');

        // Load views
        $this->loadViewsFrom(__DIR__.'/resources/views', 'dashboard');

        // Publish assets
        $this->publishes([
            __DIR__.'/public' => public_path('vendor/dashboard'),
        ], 'public');

        // Publish config
        $this->publishes([
            __DIR__.'/config/dashboard.php' => config_path('dashboard.php'),
        ]);
    }

    public function register()
    {
        // Register any package-specific services or bindings
    }
}

Remember to replace Vendor\PackageName with your actual namespace and package name. This is a simplified example, and your actual service provider might need to handle more depending on the complexity of your dashboard.

By following these steps, you can create a reusable dashboard that can be easily integrated into any Laravel project.

gych's avatar

You can add it as a module (git submodule) to the projects where you want to use this admin dashboard.

enadabuzaid's avatar

@gych and the changes can push it to my project ?

for example: I added something like logo or whatever if I want to add it on main repo not submodule

gych's avatar

@enadabuzaid By main repo you mean the dashboard repo or the project repo ?

When you make a change to the submodule those changes can be pushed and pulled to and from the dashboard repo. So all changes to the dashboard repo will be available in all the projects.

If you want to use a different logo in some projects you can use configuration for that. In your projects you add a config file for example dashboard.php where you can for example set the logo that needs to be used and some other preferences. If no logo has been set in the configuration, use a default logo.

If you expect bigger changes between the dashboard then it might be better to use multiple repo's, one for each dashboard but those should then be linked to the main dashboard repo as head repo

It can be a bit confusing to set this up in the beginning but feel free to ask questions, I'll do my best to explain it as good as possible.

1 like

Please or to participate in this conversation.