Posts: 457
Threads: 39
Joined: Dec 2016
Reputation:
17
Okay people, you clearly don’t understand how much you need this! We’re talking *automatic*, route-specific asset loading! Just drop your JS and CSS files into the folder corresponding to your controller and method and even parameters, or in the asset root to be loaded every time - done!
Go try it, star it, post some feedback here, and send me a pull request or comment with your awesome ideas, mmmmkay?
Posts: 1,062
Threads: 42
Joined: Mar 2015
Reputation:
73
Hi,
I like your approach to loading assets according to the route requested.
I have a simple asset library where the controller sets the required assets as required, and my layout library adds them to header or footer as required.
I have never really seen loading assets as an issue and I am sure lots of people have their own solutions. As I said though, I like the route specific assets idea you are using.
Paul
Posts: 655
Threads: 14
Joined: Oct 2014
Reputation:
24
I don't understand what problem this library tries to solve. How hard is it to load assets? Why would I want to depend on an external library and learn how to use it for a task as simple as loading assets?
Posts: 457
Threads: 39
Joined: Dec 2016
Reputation:
17
03-16-2019, 03:52 PM
(This post was last modified: 03-16-2019, 03:59 PM by MGatner.)
@PaulD that's a great idea using the Controller! I wasn't big on controller extensions in CI3 so I'm still getting used to them in CI4 - might be a good alternative (or addition) to the $routes variable in the optional config file.
EDIT: Is your solution open sourced anywhere that I could look at incorporating parts?
Posts: 655
Threads: 14
Joined: Oct 2014
Reputation:
24
All my controllers extends a base controller. In there I have 2 private arrays for JS and CSS files. I have functions to inject the right filenames in these array for different needs. Let say I need jQuery and Font Awesome, my controller method will call some function that will add the JS and CSS files required for jQuery and FontAwesome. When I'm ready to call the view, I call a function that creates the links to be included in the view. Then the view simply output that variable that contains the HTML snippet to include the JS and CSS files. So all I have in my controller will be something like this->addJquery(),this->addFontAwesome(), this->getHeader().
Posts: 457
Threads: 39
Joined: Dec 2016
Reputation:
17
An elegant solution! Sounds similar to what PaulD is doing, and definitely a solid way to use CI4’s native setup. Thanks for sharing it.
Posts: 457
Threads: 39
Joined: Dec 2016
Reputation:
17
03-17-2019, 03:55 PM
(This post was last modified: 03-17-2019, 04:01 PM by MGatner.)
Ah thanks for the feedback! I’m new to Packagist so I assumed that anything publically available on Packagist could take you to a repo - was that an incorrect assumption?
Anyways thanks for checking it out! Already working on the next library, for user message handling.
EDIT: Added a GitHub link to the original!