Ad

How To I Move My Javascript Resources Into A Local Folder?

- 1 answer

I wrote a small app using laravel 5.2. I tried to keep all of my code in one folder located outside the app folder.

I created a folder called modules. Inside the modules folder I have a folder for vendor name. Then, inside the vendor folder, I have a module folder which contains my code.

Here is a simple folder structure

app modules/vendor name/module name/...

I managed to move my controllers, views and a route file into the module name folder.

How would I move all of the javascripts located in the public folder into my module name folder so that everything inside is in the same folder?

Additionally, I have a lot of questions about the views being in the module folder. Is it a better practice to use $this->publishes() to publish views to the resources/views folder that comes with laravel? If so what are the benefits?

https://laravel.com/docs/5.2/packages

Ad

Answer

The benefit to publishing the views is that the end user, should they want/need to, then has the option to modify the views.

As for the javascript, its completely fine to contain the javascript in your modules folders as the docs mention. You have 2 options as to how they're then used. You can either choose to publish them which moves them into the public folder, or you can include them in your build process if you use elixir/gulp, etc. See https://laravel.com/docs/5.2/packages#public-assets

Edit:

To publish assets such as javascript files (they could in theory be anything, be that images, css, etc.) use the following from your packages service provider.

public function boot()
{
    $this->publishes([
        __DIR__ . '/path/to/script.js' => public_path('vendor/my-package'),
        __DIR___ . '/path/to/another.js' => public_path('vendor/my-package'),
    ]);
}

The above will move both script.js and another.js into the /public/vendor/my-package folder. Just to explain __DIR__ is a PHP 'magic' constant which gives the directory the current php file is located in and public_path() is a Laravel function which gives us the location of the /public folder as some users choose to rename this folder to their specific configuration.

Ad
source: stackoverflow.com
Ad