Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lumen L5 compatibility helpers. That file should be added on root path of your project... and added to your composer.json
<?php
if(!function_exists('config_path'))
{
/**
* Return the path to config files
* @param null $path
* @return string
*/
function config_path($path=null)
{
return app()->getConfigurationPath(rtrim($path, ".php"));
}
}
if(!function_exists('public_path'))
{
/**
* Return the path to public dir
* @param null $path
* @return string
*/
function public_path($path=null)
{
return rtrim(app()->basePath('public/'.$path), '/');
}
}
if(!function_exists('storage_path'))
{
/**
* Return the path to storage dir
* @param null $path
* @return string
*/
function storage_path($path=null)
{
return app()->storagePath($path);
}
}
if(!function_exists('database_path'))
{
/**
* Return the path to database dir
* @param null $path
* @return string
*/
function database_path($path=null)
{
return app()->databasePath($path);
}
}
if(!function_exists('resource_path'))
{
/**
* Return the path to resource dir
* @param null $path
* @return string
*/
function resource_path($path=null)
{
return app()->resourcePath($path);
}
}
if(!function_exists('lang_path'))
{
/**
* Return the path to lang dir
* @param null $str
* @return string
*/
function lang_path($path=null)
{
return app()->getLanguagePath($path);
}
}
if ( ! function_exists('asset'))
{
/**
* Generate an asset path for the application.
*
* @param string $path
* @param bool $secure
* @return string
*/
function asset($path, $secure = null)
{
return app('url')->asset($path, $secure);
}
}
if ( ! function_exists('elixir'))
{
/**
* Get the path to a versioned Elixir file.
*
* @param string $file
* @return string
*/
function elixir($file)
{
static $manifest = null;
if (is_null($manifest))
{
$manifest = json_decode(file_get_contents(public_path().'/build/rev-manifest.json'), true);
}
if (isset($manifest[$file]))
{
return '/build/'.$manifest[$file];
}
throw new InvalidArgumentException("File {$file} not defined in asset manifest.");
}
}
if ( ! function_exists('auth'))
{
/**
* Get the available auth instance.
*
* @return \Illuminate\Contracts\Auth\Guard
*/
function auth()
{
return app('Illuminate\Contracts\Auth\Guard');
}
}
if ( ! function_exists('bcrypt'))
{
/**
* Hash the given value.
*
* @param string $value
* @param array $options
* @return string
*/
function bcrypt($value, $options = array())
{
return app('hash')->make($value, $options);
}
}
if ( ! function_exists('redirect'))
{
/**
* Get an instance of the redirector.
*
* @param string|null $to
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
*/
function redirect($to = null, $status = 302, $headers = array(), $secure = null)
{
if (is_null($to)) return app('redirect');
return app('redirect')->to($to, $status, $headers, $secure);
}
}
if ( ! function_exists('response'))
{
/**
* Return a new response from the application.
*
* @param string $content
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Routing\ResponseFactory
*/
function response($content = '', $status = 200, array $headers = array())
{
$factory = app('Illuminate\Contracts\Routing\ResponseFactory');
if (func_num_args() === 0)
{
return $factory;
}
return $factory->make($content, $status, $headers);
}
}
if ( ! function_exists('secure_asset'))
{
/**
* Generate an asset path for the application.
*
* @param string $path
* @return string
*/
function secure_asset($path)
{
return asset($path, true);
}
}
if ( ! function_exists('secure_url'))
{
/**
* Generate a HTTPS url for the application.
*
* @param string $path
* @param mixed $parameters
* @return string
*/
function secure_url($path, $parameters = array())
{
return url($path, $parameters, true);
}
}
if ( ! function_exists('session'))
{
/**
* Get / set the specified session value.
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* @param array|string $key
* @param mixed $default
* @return mixed
*/
function session($key = null, $default = null)
{
if (is_null($key)) return app('session');
if (is_array($key)) return app('session')->put($key);
return app('session')->get($key, $default);
}
}
if ( ! function_exists('cookie'))
{
/**
* Create a new cookie instance.
*
* @param string $name
* @param string $value
* @param int $minutes
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @return \Symfony\Component\HttpFoundation\Cookie
*/
function cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
{
$cookie = app('Illuminate\Contracts\Cookie\Factory');
if (is_null($name))
{
return $cookie;
}
return $cookie->make($name, $value, $minutes, $path, $domain, $secure, $httpOnly);
}
}
@fhferreira

This comment has been minimized.

Copy link

fhferreira commented May 12, 2015

Lumen L5 compatibility helpers. That file should be added on root path of your project... and added to your composer.json

@vluzrmos

This comment has been minimized.

Copy link
Owner Author

vluzrmos commented May 12, 2015

Added more helpers, some needs to be tested.

@WRonX

This comment has been minimized.

Copy link

WRonX commented Jul 23, 2018

Hi.
getLanguagePath() is protected (at least in Lumen 5.6). I suggest:

if(!function_exists('lang_path')) 
{
    /**
     * Return the path to lang dir
     * @param null $str
     * @return string
     */
    function lang_path($path = null) {
        return resource_path() . DIRECTORY_SEPARATOR . 'lang' . ($path ? DIRECTORY_SEPARATOR . $path : $path);
    }
}
@raydvard

This comment has been minimized.

Copy link

raydvard commented Sep 13, 2018

Can you give directions on how to include this file in composer.json and where to locate the helper file concerning best practices ?
That will be helpful for newbies in lumen.

Thanks for your contribution.

@Thivieira

This comment has been minimized.

Copy link

Thivieira commented Oct 2, 2018

Can you give directions on how to include this file in composer.json and where to locate the helper file concerning best practices ?
That will be helpful for newbies in lumen.

Thanks for your contribution.

This article shows how to do it using composer:
https://laravel-news.com/creating-helpers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.