Search code examples
laraveltemplatesadminlte

Link css and js of a template (from vendor) to my views


This worked when I added the template in the public folder BUT what I m asking for is the proper way of doing things.

I created a new laravel project ,and I want to use Adminlte 3 as a template ,so I run this command that i found in the documentation :

composer require "almasaeed2010/adminlte=~3.1"

response ( I think last 2 lines maybe useful )

./composer.json has been updated
Running composer update almasaeed2010/adminlte
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking almasaeed2010/adminlte (v3.1.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading almasaeed2010/adminlte (v3.1.0)
  - Installing almasaeed2010/adminlte (v3.1.0): Extracting archive
Package swiftmailer/swiftmailer is abandoned, you should avoid using it. Use symfony/mailer instead.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: laravel/ui
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
77 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
No publishable resources for tag [laravel-assets].
Publishing complete.

I found out that a new folder was added to my vendor folder .
Everything is downloaded correctly , so I tried to create a Master page that contains all the repeated elements of the dashboard but , the links I used are not dynamic so I m not sure if its gonna cause problems if the views are in different directory level. for example :

this is the Admin lte 3 link to css :

  <link rel="stylesheet" href="../../dist/css/adminlte.min.css">

how I called it like this :

  <link rel="stylesheet" href="{{ require('C:\Users\Yasser-Ch\Desktop\LaravelProject\gestionDivers\vendor\almasaeed2010\adminlte\dist\css\adminlte.min.css') }}">

and this made the page look like the css file so tried put it inside the resources\css\app.css and of course it didn't work because css is not .blad.php ,so I thought maybe composer ( because in the documentation says exactly :

The vendor directory contains your Composer dependencies.

) or something else can interpret it and put only what I need from the "vendor\almasaeed2010" (the template in general ) and put it in 2 files 1 for css and 1 for js ... I did some researches and I found mix …
So I tried this :

  {{ mix.postCss('\vendor\almasaeed2010\adminlte\dist\css\adminlte.min.css', 'public/css') }}
  <link rel="stylesheet" href="adminlte.min.css">

Then I run these commands :

  npm install // to download mix that is by default already required in the package.json 
  npm run dev // and here I expect mix to start mixing

I check the public/css has only app.css added . And when I re run the website using php artisan serve I get an error that says mix is undefined .
I mean mix didn't cause an error during npm run dev .I would delete it ,if it did the job of mixing before starting the server . How to link the css/js from the vendor ? should I publish everything inside the public folder ? Because I think the fact that they put the template inside the vendor folder means it shouldn't be directly in the public directory.


Solution

  • You can use webpack for that, check file webpack.mix.js add or search line like below

    // webpack.mix.js
    mix.sass('resources/sass/app.scss', 'public/css')
    
    // in 'app.scss
    // import all necessary css files 
    @import 'vendor\almasaeed2010\adminlte\dist\css\adminlte.min.css'
    
    

    run npm install if never, then run npm run dev or npm run prod. It will create app.css in public/css

    <link href="{{ asset('css/app.css') }}" rel="stylesheet">