The Blog

Laravel

Laravel Application Helper: app_path

John Koster
John Koster
November 20, 2016

The app_path function in Laravel returns the full path to the application directory. It can also be used to build paths relative to the application directory. However, it does not automatically add a trailing forward slash to the path. To add a trailing forward slash, the str_finish function can be used.

Laravel

Laravel Application Helper: auth

John Koster
John Koster
November 20, 2016

The auth helper function in Laravel is used to retrieve an instance of Illuminate\Contracts\Auth\Factory, which is responsible for handling authentication. It is an alternative to using the Auth facade. If no arguments are provided, auth() returns the currently logged in user. You can also specify a custom authentication guard by passing the guard name as an argument. When $guard is set to null, the AuthManager instance uses the default guard specified in the configuration file.

Laravel

Laravel Application Helper: base_path

John Koster
John Koster
November 20, 2016

The base_path function in Laravel retrieves the path to the directory where the application is installed. It can also be used to construct paths relative to the base path. In the given example, the base_path function is used to retrieve the base path and construct paths for the app and public directories. Note that the function does not automatically add a trailing slash to the path.

Laravel

Laravel Application Helper: config_path

John Koster
John Koster
November 20, 2016

The config_path function in Laravel retrieves the path to the config directory and can be used to construct paths relative to it. An optional $path parameter allows for specific file paths to be constructed. In this example, the resulting paths are displayed as comments above the function calls.

Laravel

Laravel Application Helper: database_path

John Koster
John Koster
November 20, 2016

The database_path function in Laravel retrieves the path to the database directory. By supplying a $path, you can construct paths relative to the database directory. For example, calling database_path() will return the path to the /home/vagrant/Code/Laravel/database directory, while database_path('migrations') will return the path to the /home/vagrant/Code/Laravel/migrations directory. Additionally, the str_finish function can be used to ensure a trailing slash is added to the path, as shown in the example str_finish(database_path('seeds'), '/').

Laravel

Laravel Application Helper Function: abort

John Koster
John Koster
November 20, 2016

The abort function is used to throw an HTTP exception with the given code, message, and headers. This exception can be caught and handled in the application's kernel. If the code is 404, the function will return a "Not Found" exception using the provided message. In the example, if the $user object does not have the admin property set to true, the code will abort with a 401 unauthorized access error.

Laravel

Laravel Application Helper Function: back

John Koster
John Koster
November 20, 2016

The back helper function in Laravel creates a redirect response to the user's previous location. It takes two optional parameters to control the status code and headers of the response. You can change the status code to be either "Found" or "Moved Permanently" and you can supply additional headers. There are different ways to return a redirect response using the Redirect facade, the response helper function, or the back helper function itself. You can also change the status code or supply additional headers as needed.

Laravel

Laravel Application Helper Function: dispatch

John Koster
John Koster
November 20, 2016

The dispatch helper function allows you to easily push a new job onto the job queue. It resolves the Dispatcher instance from the Service Container and calls the dispatch method. You can also use app(Dispatcher::class)->dispatch($job) or Bus::dispatch($job) as alternatives to the helper function.

Laravel

Laravel Application Helper Function: event

John Koster
John Koster
November 20, 2016

The event helper is a useful function for dispatching events and notifying their listeners. It allows you to pass a payload of data when dispatching the event, and the responses from the listeners are stored in an array. By setting the $halt parameter to true, the dispatcher will stop calling listeners after a non-null response is received. You can fire an event by specifying the class name or by instantiating a new instance of the event class itself. If no payload is supplied, the event instance itself will be used as the payload.

Laravel

Laravel Application Helper Function: factory

John Koster
John Koster
November 20, 2016

The factory() function is used to create Eloquent models, primarily for testing applications. This method allows you to generate one or more instances of a specific model, with the option to apply modifications previously defined using the bind method. Example uses include returning a single instance of a user model, returning multiple instances, or applying modifications bound to a specific description like 'admin'.

Latest posts

That Escalated Quickly: All the New Things

The past six months or so have been incredibly busy. What started as a new article series about cust...

Read more
Troubleshooting a Statamic Illegal Offset Type Error when Viewing Collection Entries in the Control Panel

In this post I talk about how I resolved a mysterious illegal offset type error when viewing collect...

Read more
Creating Simple HTTP Redirect Routes from a Statamic Site

Generating a custom Laravel routes file from a Statamic website to redirect to a new domain.

Read more
Disabling Vite File Hashes

Disabling file hashes in Vite output can be accomplished by modifying your project's vite.config.js

Read more
Implementing a Custom Laravel Blade Precompiler for Volt and Livewire

Learn how to implement a custom component compiler for Laravel's Blade templating language in this p...

Read more
Creating a Hybrid Cache System for Statamic: Part Five

Part 5 of 6 covers implementing a cache namespace and labeling system, which we can use to target mu...

Read more