3

I have problem to provide assets such as images, css, javascript when using module. Each module has each set of assets such as javascript, css, images inside of resources folder of that particular module and that i just compiled to inside of public folder of module. I have problem to provide to link to that assets.

I provide the link like this way

<link href="{{ asset('modules/robust/core/public/admin/css/app.css') }}" rel="stylesheet">

It shows this kind of result enter image description here

I think i should define the custom path for the module assets But i do not know how to do that. Please help me.

Folder structure is like this.

enter image description here

Madhu Sudhan Subedi
  • 469
  • 1
  • 6
  • 13
  • Can you please explain what you mean by **I have problem to provide assets such as images, css, javascript when using module.** What module are you talking about? – prateekkathal Sep 10 '16 at 05:53
  • i am talking about custom module using laravel for e.g. blog, product, page modules etc. Each module has it's own assets path. – Madhu Sudhan Subedi Sep 10 '16 at 07:18
  • Can you try using `{{ url('modules/robust/core/public/admin/css/app.css') }}` method once? – prateekkathal Sep 10 '16 at 07:21
  • @prateekkathal folder structure is like this which i include above. – Madhu Sudhan Subedi Sep 10 '16 at 07:22
  • yes i tried but not working, not found exception in console. – Madhu Sudhan Subedi Sep 10 '16 at 07:25
  • did u cheked inspect element ? path is correct ? – Hamelraj Sep 10 '16 at 07:28
  • @MadhuSudhanSubedi Yes, a custom path will be required to access files for that. May I know why you are trying to use a separate folder and not the public folder for this? – prateekkathal Sep 10 '16 at 07:30
  • yes i checked path is http://localhost:8000/modules/robust/core/public/admin/css/app.css I think i should define public path for the core module first , i do not know i can i do that would you like to help me ? – Madhu Sudhan Subedi Sep 10 '16 at 07:31
  • @prateekkathal I do not know what is the best way to make modular application in laravel. if i put separate assets inside resources/assets folder of each module and compile them to the default public folder of laravel it will work for all modules i just afraid of that is the best way ? Please suggest me ! sorry for poor english. – Madhu Sudhan Subedi Sep 10 '16 at 07:38
  • @MadhuSudhanSubedi If you want to make a modular application, I'd suppose you read [this answer](http://stackoverflow.com/questions/28485690/how-to-structure-a-modular-app-in-laravel-5) once. Also, I suggest you **separate out images/css/js files in public folder** and put only routes & views in the **app/Modules/blog** or **app/Modules/anything**. :) – prateekkathal Sep 10 '16 at 07:45
  • @prateekkathal ok thanks for the suggestion, Now i do not need to override the publicPath for each module i will configure it in modular way inside of root-directory/public directory. – Madhu Sudhan Subedi Sep 10 '16 at 08:53
  • @MadhuSudhanSubedi Cool! Please accept my answer :) – prateekkathal Sep 10 '16 at 10:00
  • @prateekkathal I accepted :D. my question is getting -1 vote please and make it 0 :) . – Madhu Sudhan Subedi Sep 10 '16 at 17:01

4 Answers4

3

Use this command :

php artisan module:publish 

and get assets files like this :

{{ Module::asset('blog:js/script.js') }}
Zahra Badri
  • 1,656
  • 1
  • 17
  • 28
2

As per our conversation in the comments, I suggest you divide your project in the following manner

  • app/Modules/

    • blog/
    • core/
    • etc.../
  • public/

    • blog/css
    • blog/js
    • blog/images
    • core/css
    • core/js
    • core/images
    • etc.../css
    • etc.../js
    • etc.../images

And then use commands like this in Controller

$this->loadViewsFrom()

and in View, you can use this

{{ asset('blog/images/myimage.jpg') }}

If there are are more issue, please let me know by commenting below :)

Glad I could be of help :D

prateekkathal
  • 3,482
  • 1
  • 20
  • 40
2

You can create a route path in route.php file:

Route::get('/assets/{module}/{type}/{file}', [ function ($module, $type, $file) {
    $module = ucfirst($module);

    $path = app_path("Modules/$module/Resources/Blocks/$type/$file");

    if (\File::exists($path)) {
        return response()->download($path, "$file");
    }

    return response()->json([ ], 404);
}]);
Abdulla
  • 485
  • 6
  • 16
0
{{ Module::asset('blog:js/script.js') }}

This assumes your script is located at /Modules/ModuleName/Assets/js/script.js. If it is not in a folder, just omit the js/ part in the asset call.

Olufemi Ayodele
  • 385
  • 2
  • 8