Globally accessible variable for setting paths - Laravel 4

381 Views Asked by At

setting up Laravel 4 and looking for a way too sort out the base path for local vs live development.

My website currently runs on 'www.project.com' and then I access the site on local machine as 'localhost:8888/project'.

For things such as CSS and image paths etc I am looking for a way to have a variable for the base paths so I dont have to manually go changing paths etc when switching to live host.

I currently use the environment for my database settings:

$env = $app->detectEnvironment(array(

    'local' => array('MacBook.local'),
    'production' => array('project.com'),

));

Can I work with something similar to this so its all done automatically?

My css paths in my template blade file are:

'/css/main.css'

And then on local:

'/project/public_html/css/main.css'

Unless there is a way to get the route of what I am trying to do with PHP?

Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER

You may use something like this:

<!--Blade-->
<link href="{{ asset('css/main.css') }}" rel="stylesheet" />

<!--Non-Blade-->
<link href="<?php echo asset('css/main.css') ?>" rel="stylesheet" />

This will solve the problem. Check the asset helper function on the documentation.

0
On

You could use App::environment() to check the current environment and then set the appropriate base path for all assets using the html base tag. Then, whenever you want to reference an asset, just reference it relative to this base path and the browser will do the rest.

This way, you'd end up with something like:

Production:

<base href="http://www.project.com/">

Local:

<base href="http://localhost:8888/project/public_html/">

Obviously you'd want to use Laravel's URL functions to build these URLs rather than hard-code them though...