Why are assets being loaded at the controller?



  • I was wondering why are you loading assets (CSS/JS) in the controller with assetPipeline?

    Wouldn't it be more future proof and also better to have assets being called directly from the views?


  • admin

    Hello,

    This is how it was first done. But handling the assets via the AssetPipeline and AssetManager makes it easy for everyone to add new assets without modifying the theme. This an important factor as the theme may be a theme pulled from composer which can't be changed.



  • I remember a package from the early 4.0 series and liked it but eventually dropped it because I ended with the possibility of swapping out different libraries. Like select2 and Chosen. Also, I changing up CSS was easier.

    I do like you assetPipeline and assetManger!

    One thing that I find possibly being an issue is like you said that a theme is being called from composer but so are modules. If I were working with only the front end, I'd rather have easy access to what is being called instead of having to dig through controllers to find them. Which brings up the idea that IF a designer wanted to do something more "creative" they are stuck with the choices that are made rather than being able to make those changes on views only. I've also noticed that a lot of people over at Laracast just call them from the view now compared to 2 years ago.

    The last thing I was thinking is that should a controller really control a view asset?

    I'm just trying to throw out some ideas and opinions.


  • admin

    The controller doesn't control the view assets. The pipeline does. As I explained this is needed to add assets without editing the theme, we're talking about backend theme.

    The front end theme, the ones that your frontend developer will be working on works just as usual if you want (or you can also use the pipeline, however you prefer).

    I suggest you take a look at the default backend and front end theme.



  • Thank you for the clarification.

    I realize that the pipeline does the actual work but the assets are being "set" in the controller which is what I'm talking about.

    I am slowly going through the fronted theme. The back end is going to be my next step.

    Thanks!


  • admin

    No, the assets are not set in the controller.

    Only assets in controller are exceptional ones that are only needed for a couple of views.



  • Both of us follow a similar pattern for the master template.

    Here is an example of the file I had open.

        /**
         * Show the form for editing the specified resource.
         *
         * @param Page $page
         * @return Response
         */
        public function edit(Page $page)
        {
            $this->assetPipeline->requireJs('ckeditor.js');
    
            return view('page::admin.edit', compact('page'));
        }
    

    Here is an example of how I've been doing it.

    @section('styles')
    	<link rel="stylesheet" type="text/css" href="{{ asset('assets/vendors/chosen_v1.4.2/chosen.min.css') }}">
    	<link rel="stylesheet" type="text/css" href="{{ asset('assets/css/chosen_bootstrap.css') }}">
    @stop
    
    @section('scripts')
    	<script type="text/javascript" src="{{ asset('assets/vendors/chosen_v1.4.2/chosen.jquery.min.js') }}"></script>
    @stop
    
    @section('inline-scripts')
    	jQuery(document).ready(function($) {
    		$(".chosen-select").chosen({
    			width: "100%"
    		});
    	});
    @stop
    

    I do it this way because when you edit the template you know exactly what script is being called and what extra javascript is need.

    Both of our methods are actually very similar but I just don't have any css or js files in the controller.

    My idea is that if I wanted to change the javascript being used I'd look for it in the template. Unless, I only want 1 editor to be used because it is used across the whole application and requires a lot. I'm thinking your media module is something like that ... I still haven't had a chance to look at it.


  • admin

    We already went over this. Let's not go in circles. There's a clear reason why.


Log in to reply
 

Looks like your connection to AsgardCms was lost, please wait while we try to reconnect.