1

Is there a helper in Laravel 5.0 that automatically adds http to a url without it? This is similar to codeigniter's prep_url that could be found here.

1

1 Answer 1

6

No but you can add it yourself. In your composer.json file add a files key under autoload and point it your helper file e.g.

"autoload": {
    "files": [
        "app/helpers.php"
    ]
}

Then create app/helpers.php with the code (lifted from https://github.com/bcit-ci/CodeIgniter/blob/master/system/helpers/url_helper.php):

<?php

if ( ! function_exists('prep_url'))
{
    /**
     * Prep URL
     *
     * Simply adds the http:// part if no scheme is included
     *
     * @param   string  the URL
     * @return  string
     */
    function prep_url($str = '')
    {
        if ($str === 'http://' OR $str === '')
        {
            return '';
        }
        $url = parse_url($str);
        if ( ! $url OR ! isset($url['scheme']))
        {
            return 'http://'.$str;
        }
        return $str;
    }
}

Now you have prep_url globally accessible! Don't forget to run a composer dump-autoload too.

Sign up to request clarification or add additional context in comments.

1 Comment

How can i use it inside my view?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.