Skip to content

Instantly share code, notes, and snippets.

@atorscho
Last active August 29, 2015 14:26
Show Gist options
  • Save atorscho/5bcf63d077c11ed0e8ce to your computer and use it in GitHub Desktop.
Save atorscho/5bcf63d077c11ed0e8ce to your computer and use it in GitHub Desktop.
Assertion and check helper functions for Laravel.
<?php
namespace App\Helpers;
use Request;
use Route;
use URL;
use View;
class AssertionHelper
{
/**
* Return true if current page is home.
*
* @return bool
*/
public static function isHome()
{
return Route::is('home');
}
/**
* Return true if current page is an admin home page.
*
* @return bool
*/
public static function isAdminHome()
{
return URL::current() == route('admin.index');
}
/**
* Return true if current page is an admin page.
*
* @return bool
*/
public static function isAdmin()
{
return Route::is('*admin*');
}
/**
* Return true if current page is $page.
*
* @param string $page
* @param array $parameters
*
* @return bool
*/
public static function isPage($page, $parameters = [])
{
// Check if $page is a route name
if (Route::has($page)) {
if ($parameters) {
return URL::current() == route($page, $parameters);
}
return Route::currentRouteName() == $page;
}
$current = str_replace(Request::root() . '/', '', URL::full());
return $current == $page;
}
/**
* Check if yield section exists.
*
* @param string $yield
*
* @return bool
*/
public static function yieldExists($yield)
{
return (bool) trim(View::yieldContent($yield));
}
}
<?php
use App\Helpers\AssertionHelper;
if ( !function_exists('is_home') )
{
/**
* Return true if current page is home.
*
* @return bool
*/
function is_home()
{
return AssertionHelper::isHome();
}
}
if ( !function_exists('is_admin') )
{
/**
* Return true if current page is an admin page.
*
* @return bool
*/
function is_admin()
{
return AssertionHelper::isAdmin();
}
}
if ( !function_exists('is_admin_home') )
{
/**
* Return true if current page is an admin home page.
*
* @return bool
*/
function is_admin_home()
{
return AssertionHelper::isAdminHome();
}
}
if ( !function_exists('is_page') )
{
/**
* Return true if current page is $page.
*
* @param string $page
* @param array $parameters
*
* @return bool
*/
function is_page($page, $parameters = [])
{
return AssertionHelper::isPage($page, $parameters);
}
}
if (!function_exists('yield_exists')) {
/**
* Check if yield section exists.
*
* @param string $yield
*
* @return bool
*/
function yield_exists($yield)
{
return AssertionHelper::yieldExists($yield);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment