Eddy Freeman
Eddy Freeman

Reputation: 3309

Referencing files in Laravel 4 public folder

Am newbie in laravel and having troubles referencing my angularJS files in Laravel's "public" folder. My public folder structure looks like this::

public
   -angular
      -js

I have other folders inside the js folder as well. I have my index.php file inside the app/views folder. When I go to the home page of my application http://learninglaravel.dev, the browser brings me to the index.php file as expected.

Inside my "index.php" file, I want to reference my javascript files under the "public/angular" folder but I get "NetworkError: 404 Not Found" - in the browser console meaning that those files can't be found. These are some of the options I have tried to include them in the "index.php" file:

<script src="../../public/angular/js/angular.js"></script>
<script src="../../public/angular/js/controllers/mainCtrl.js"></script> 
or
<script src="angular/js/angular.js"></script>
<script src="angular/js/controllers/mainCtrl.js"></script>
or
<script src="./angular/js/angular.js"></script>
<script src="./angular/js/controllers/mainCtrl.js"></script>

All these options are not able to find the files under the "public" folder.

I am not using Blade template.

Upvotes: 1

Views: 411

Answers (2)

James
James

Reputation: 16359

Just to expand on the answer given by Eduardo, the asset URL helper is what you're after.

Essentially it generates a dynamic URL to your public folder which you can then use for any additional scripts, CSS or miscellaneous stuff you wish to include.

In this example, you would want to use something like the below given your file structure:

<script src="{{ asset('angular/js/angular.js') }}"></script>
<script src="{{ asset('angular/js/controllers/mainCtrl.js') }}"></script>

As long as you are using the blade template engine (i.e index.blade.php) then this would output on your page to:

<script src="http://yourwebsite.com/angular/js/angular.js"></script>
<script src="http://yourwebsite.com/angular/js/controllers/mainCtrl.js') }}"></script>

Where your Laravel application will be able to resolve it to the script needed.

For more info just check out the docs.

Update

Have just seen your comment that you aren't using blade.

It wouldn't be hard to transfer what you have to blade, just copy it all in to a new file and save it as .blade.php and update your references in controllers.

Otherwise given the URL it generates, you should be able to put these together manually and you can test them simply by navigating to that URL to see what you get.

I would strongly advise using blade though, it will make life with Laravel much easier given all the helpers it has and the syntax you can use.

Upvotes: 1

Eduardo Pacios
Eduardo Pacios

Reputation: 1865

You should try this (assuming your filename is js.js):

<script src="{{ asset('angular/js.js') }}"></script>

Are you using blade?

Upvotes: 1

Related Questions