This repository contains a collection of helpful PHP functions designed to be highly reusable and flexible, especially for Laravel developers who need to work with non-Laravel, plain PHP code.
To include these functions in your project, simply download or clone this repository and include the functions.php
file in your project:
require_once 'path/to/functions.php';
Retrieve the value of a request variable from $_REQUEST
.
$value = request('items'); // if items is not set, $value will be null
$value = request('items', 'default value'); // if items is not set, $value will be 'default value'
$value = request(['items', 'other']); // if items is not set, $value will be ['items' => null, 'other' => null]
Dump the given value and die.
dd($myVariable); // Dumps the variable and stops execution
Dump the given value.
dump($myVariable); // Outputs the contents of $myVariable in a readable format
Retrieve the value of an environment variable.
$appEnv = env('APP_ENV', 'production'); // Gets the value of APP_ENV or 'production' if not set
Retrieve the value of a configuration setting.
$dbHost = config('database.host', 'localhost'); // Gets the value of database.host or 'localhost' if not set
Redirect to a specified URL.
redirect('https://example.com'); // Redirects to the specified URL
Generate a URL for an asset.
$assetUrl = asset('images/logo.png'); // Generates the URL for the asset
Retrieve an old input value.
$oldValue = old('username'); // Gets the old input value for 'username'
Generate a CSRF token field.
echo csrf_field(); // Outputs the CSRF token field
Generate a hidden input field for the HTTP method.
echo method_field('PUT'); // Outputs the hidden input field for the PUT method
Handle routing for the request.
route(); // Handles the routing for the request
Get the authenticated user.
$user = auth(); // Gets the authenticated user
Abort the request with a specified status code and message.
abort(404, 'Not Found'); // Aborts the request with a 404 status code and message
Get or set session values.
$value = session('key'); // Gets the session value for 'key'
session(['key' => 'value']); // Sets the session value for 'key'
Get a value from the $_SERVER
superglobal.
$host = server('HTTP_HOST'); // Gets the value of HTTP_HOST from the $_SERVER superglobal
Get a value from an array or object using dot notation.
$value = data_get($array, 'key'); // Gets the value of 'key' from the array
Return the default value of the given value.
$default = value($someValue); // Returns the default value of $someValue
Simple cache helper function to store and retrieve data from a file-based cache.
cache('my_key', 'my_value', 1800); // Cache 'my_value' for 1800 seconds (30 minutes)
$value = cache('my_key'); // Retrieve the cached value
Set a value within an array or object using dot notation.
data_set($array, 'key', 'value'); // Sets the value of 'key' in the array
Encrypt the given value.
$encrypted = encrypt('my_secret_value'); // Returns the encrypted string
Decrypt the given value.
$decrypted = decrypt($encryptedValue); // Returns the decrypted value
Log an informational message.
info('User logged in', ['user_id' => 123]); // Logs the message with context
Log a message.
logger('info', 'User logged in', ['user_id' => 123]); // Logs the message with context
Create an HTTP response.
response('Hello, World!', 200, ['Content-Type' => 'text/plain']); // Creates a response
Create a JSON HTTP response.
json_response(['success' => true, 'message' => 'Data saved successfully'], 200); // Creates a JSON response
Get the storage path.
$path = storage('uploads/myfile.txt'); // Returns the full path to the specified file in the storage directory
Generate a URL.
echo url('user/profile'); // Returns the full URL to the user profile page
echo url('search', ['q' => 'laravel']); // Returns the URL with query parameters
echo url('user/profile', [], true); // Returns a secure HTTPS URL to the user profile page
Render a view template.
view('home', ['name' => 'John']); // Renders the 'home' view with the provided data
Pluck an array of values from an array.
$array = [
['name' => 'John', 'age' => 30],
['name' => 'Jane', 'age' => 25],
];
$names = array_pluck($array, 'name'); // Returns ['John', 'Jane']
Get all of the given array except for a specified array of keys.
$array = ['name' => 'John', 'age' => 30, 'location' => 'NY'];
$result = array_except($array, ['age', 'location']); // Returns ['name' => 'John']
Get a subset of the items from the given array.
$array = ['name' => 'John', 'age' => 30, 'location' => 'NY'];
$result = array_only($array, ['name', 'location']); // Returns ['name' => 'John', 'location' => 'NY']
This project is licensed under the MIT License - see the LICENSE file for details.