Download the PHP package ashallendesign/short-url without Composer
On this page you can find all versions of the php package ashallendesign/short-url. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ashallendesign/short-url
More information about ashallendesign/short-url
Files in ashallendesign/short-url
Package short-url
Short Description A Laravel package for creating shortened URLs for your web apps.
License MIT
Homepage https://github.com/ash-jc-allen/short-url
Informations about the package short-url
Table of Contents
- Overview
- Installation
- Requirements
- Install the Package
- Publish the Config and Migrations
- Migrate the Database
- Usage
- Building Shortened URLs
- Quick Start
- Custom Keys
- Tracking Visitors
- Enabling Tracking
- Tracking IP Address
- Tracking Browser & Browser Version
- Tracking Operating System & Operating System Version
- Tracking Device Type
- Tracking Referer URL
- Custom Short URL Fields
- Single Use
- Enforce HTTPS
- Forwarding Query Parameters
- Redirect Status Code
- Activation and Deactivation Times
- Using a Custom Seed
- Facade
- Conditionals
- Using the Shortened URLs
- Default Route and Controller
- Custom Route
- Tracking
- Customisation
- Disabling the Default Route
- Default URL Key Length
- Tracking Visits
- Default Tracking
- Tracking Fields
- Config Validation
- Custom Database Connection
- Specifying the Key Generator
- Specifying the User Agent Parser
- Specifying the Allowed URL Schemes
- Helper Methods
- Visits
- Find by URL Key
- Find by Destination URL
- Tracking Enabled
- Tracked Fields
- Events
- Short URL Visited
- Model Factories
- Building Shortened URLs
- Testing
- Security
- Contribution
- Credits
- Changelog
- Upgrading
- License
Overview
A Laravel package that can be used for adding shortened URLs to your existing web app.
Installation
Requirements
The package has been developed and tested to work with the following minimum requirements:
- PHP 8.0
- Laravel 8.0
Short URL requires either the BC Math or GMP PHP extensions in order to work.
Install the Package
You can install the package via Composer:
Publish the Config and Migrations
You can then publish the package's config file and database migrations by using the following command:
Migrate the Database
This package contains two migrations that add two new tables to the database: and . To run these migrations, simply run the following command:
Usage
Building Shortened URLs
Quick Start
The quickest way to get started with creating a shortened URL is by using the snippet below. The ->make()
method
returns a ShortURL model that you can grab the shortened URL from.
Custom Keys
By default, the shortened URL that is generated will contain a random key. The key will be of the length that you define in the config files (defaults to 5 characters). Example: if a URL is , the key is .
You may wish to define a custom key yourself for that URL that is more meaningful than a randomly generated one. You can do this by using the method. Example:
Note: All of the URL keys are unique, so you cannot use a key that already exists in the database for another shortened URL.
Tracking Visitors
You may want to track some data about the visitors that have used the shortened URL. This can be useful for analytics. By default, tracking is enabled and all of the available tracking fields are also enabled. You can toggle the default options for the different parts of the tracking in the config file. Read further on in the Customisation section to see how to customise the default tracking behaviours.
Note: Even if the tracking options (such as ) are enabled for a short URL, they won't be recorded unless the options is enabled. This can come in handy if you want to enable/disable tracking for a short URL without needing to individually set each option.
Enabling Tracking
If you want to override whether if tracking is enabled or not when creating a shortened URL, you can use the method. This method accepts a boolean but defaults to if a parameter is not passed.
The example below shows how to enable tracking for the URL and override the config variable:
The example below shows how to disable tracking for the URL and override the default config variable:
Tracking IP Address
If you want to override whether if IP address tracking is enabled or not when creating a shortened URL, you can use the method. This method accepts a boolean but defaults to if a parameter is not passed.
The example below shows how to enable IP address tracking for the URL and override the default config variable:
Tracking Browser & Browser Version
If you want to override whether if browser name and browser version tracking is enabled or not when creating a shortened URL, you can use the and methods. This method accepts a boolean but defaults to if a parameter is not passed.
The example below shows how to enable browser name tracking for the URL and override the default config variable:
The example below shows how to enable browser version tracking for the URL and override the default config variable:
Tracking Operating System & Operating System Version
If you want to override whether if operating system name and operating system version tracking is enabled or not when creating a shortened URL, you can use the and methods. These methods accept a boolean but default to if a parameter is not passed.
The example below shows how to enable operating system name tracking for the URL and override the default config variable:
The example below shows how to enable operating system version tracking for the URL and override the default config variable:
Tracking Device Type
If you want to override whether if device type tracking is enabled or not when creating a shortened URL, you can use the method. This method accepts a boolean but defaults to if a parameter is not passed.
The example below shows how to enable device type tracking for the URL and override the default config variable:
Tracking Referer URL
If you want to override whether if referer URL tracking is enabled or not when creating a shortened URL, you can use the method. This method accepts a boolean but defaults to if a parameter is not passed.
The example below shows how to enable referer URL tracking for the URL and override the default config variable:
Custom Short URL Fields
There may be times when you want to add your own custom fields to the ShortURL model and store them in the database. For example, you might want to associate the short URL with a tenant, organisation, user, etc.
To do this you can use the beforeCreate
method when building your short URL. This method accepts a closure that receives the AshAllenDesign\ShortURL\Models\ShortURL
model instance before it's saved to your database.
The example below shows how to add a tenant_id
field to the AshAllenDesign\ShortURL\Models\ShortURL
model:
Please remember that to store custom fields in the database, you'll have to make sure those fields are added to the short_urls
table. You can do this by creating a new migration that adds the fields to the table, or by updating the migrations that ship with this package.
Single Use
By default, all of the shortened URLs can be visited for as long as you leave them available. However, you may want to only allow access to a shortened URL once. Then any visitors who visit the URL after it has already been viewed will get a HTTP 404.
To create a single use shortened URL, you can use the method.
The example below shows how to create a single use shortened URL:
Enforce HTTPS
When building a shortened URL, you might want to enforce that the visitor is redirected to the HTTPS version of the destination URL. This can be particularly useful if you're allowing your web app users to create their own shortened URLS.
To enforce HTTPS, you can use the method when building the shortened URL.
The example below shows how to create a secure shortened URL:
Forwarding Query Parameters
When building a short URL, you might want to forward the query parameters sent in the request to destination URL. By default, this functionality is disabled, but can be enabled by setting the forward_query_params
config option to true
.
Alternatively, you can also use the ->forwardQueryParams()
method when building your shortened URL, as shown in the example below:
Based on the example above, assuming that the original short URL's destination_url
was https://destination.com
, making a request to https://webapp.com/short/xxx?param1=abc¶m2=def
would redirect to https://destination.com?param1=test¶m2=def
Redirect Status Code
By default, all short URLs are redirected with a HTTP status code. But, this can be overridden when building the shortened URL using the method.
The example below shows how to create a shortened URL with a redirect HTTP status code of :
Activation and Deactivation Times
By default, all short URLs that you create are active until you delete them. However, you may set activation and deactivation times for your URLs when you're creating them.
Doing this can be useful for marketing campaigns. For example, you may want to launch a new URL for a marketing campaign on a given date and then automatically deactivate that URL when the marketing campaign comes to an end.
The example below shows how to create a shortened URL that will be active from this time tomorrow onwards:
The example below shows how to create a shortened URL that will be active from this time tomorrow onwards and then is deactivated the day after:
Using a Custom Seed
By default, the package will use the ID of the last inserted short URL as the seed for generating a short URL's key. In some cases, you may want to use a custom seed instead. To do this, you can pass an integer to the generateKeyUsing
method like so:
Facade
If you prefer to use facades in Laravel, you can choose to use the provided facade instead of instantiating the class manually.
The example below shows an example of how you could use the facade to create a shortened URL:
Conditionals
The Builder
class uses the Illuminate\Support\Traits\Conditionable
trait, so you can use the when
and unless
methods when building your short URLs.
For example, let's take this block of code that uses if
when building the short URL:
This could be rewritten using when
like so:
Using the Shortened URLs
Default Route and Controller
By default, the shortened URLs that are created use the package's route and controller. The routes use the following structure: . This route uses the single-use controller that is found at .
Custom Route
You may wish to use a different routing structure for your shortened URLs other than the default URLs that are created. For example, you might want to use or . You can customise this to suit the needs of your project.
To use the custom routing all you need to do is add a web route to your project that points to the ShortURLController and uses the field.
The example below shows how you could add a custom route to your file to use the shortened URLs:
Note: If you use your own custom routing, you might want to disable the default route that the app provides. Details are provided for this in the Customisation section below.
Tracking
If tracking is enabled for a shortened URL, each time the link is visited, a new ShortURLVisit row in the database will be created. By default, the package is set to record the following fields of a visitor:
- IP Address
- Browser Name
- Browser Version
- Operating System Name
- Operating System Version
- Referer URL (the URL that the visitor originally came from)
- Device Type (can be: ///)
Each of these fields can be toggled in the config files so that you only record the fields you need. Details on how to do this are provided for this in the Customisation section below.
Customisation
Customising the Default Route
Customising the Default URL
The package comes with a route that you can use for your short URLs. By default, this route uses your Laravel app's app.url
config field to build the URL.
However, you might want to override this and use a different URL for your short URLs. For instance, you might want to use a different domain name for your short URLs.
To override the base URL, you can set the default_url
config field. For example, to set the base URL to https://example.com
, you can set the default_url
in your config/short-url.php
file like so:
To use the your application's app.url
config field, you can set the short_url.default_url
field to null
.
Customising the Prefix
The package comes with a route that you can use for your short URLs. By default, this route is /short/{shortURLKey}
.
You might want to keep using this default route but change the /short/
prefix to something else. To do this, you can change the prefix
field in the config.
For example, to change the default short URL to /s
, you could change the config value like so:
Removing the Prefix
You may also remove the prefix from the default route completely. For example, if you want your short URL to be accessible via /{shortUrlKey}
, then you can update the prefix
config value to null
like so:
Defining Middleware
You may wish to run the default short URL through some middleware in your application. To do this, you can define the middleware that the route should use via the middleware
config value.
For example, if you have a MyAwesomeMiddleware
class, you could update your short-url
config like so:
You can also use this same approach to define middleware groups rather than individual middleware classes. For example, if you want your default short URL route to use the web
middleware group, you could update your config like so:
It's important to note that this middleware will only be automatically applied to the default short URL route that ships with the package. If you are defining your own route, you'll need to apply this middleware to your route yourself.
Disabling the Default Route
If you have added your own custom route to your project, you may want to block the default route that the package provides. You can do this by setting the following value in the config:
If the default route is disabled, any visitors who go to the route will receive a HTTP 404.
You may want to manually prevent the route from being automatically registered and manually register it yourself in your own routes file. To do this you can add the following code to your routes file (e.g. web.php
):
Default URL Key Length
When building a shortened URL, you have the option to define your own URL key or to randomly generate one. If one is randomly generated, the minimum length of it is determined from the config.
A minimum key length of 3 has been enforced for performance reasons.
For example, to create a shortened URL with a key length of 10 characters, you could set the following in the config:
By default, the shortened URLs that are created have a key length of 5.
Please be aware that the key length that you specify in the config is only a desirable length. It acts as a minimum length rather than a fixed length. For example, if the is set to 3 in the config and there is a unique 3 character long key that hasn't been used yet, the key created will be 3 characters long. However, if all of the possible 3 character long keys are taken, a 4 character key will be created.
The Hashids library is used to assist with creating the URL keys.
Tracking Visits
By default, the package enables tracking of all the available fields on each URL built. However, this can be toggled in the config file.
Default Tracking
To disable tracking by default on all future short URLs that are generated, set the following in the config:
Note: Disabling tracking by default won't disable tracking for any shortened URLs that already exist. It will only apply to all shortened URLs that are created after the config update.
Tracking Fields
You can toggle the default options for each of fields that can be tracked by changing them in the config. These options can then be overridden for each short URL at the point of creation, as shown in the Tracking Visitors section.
For example, the snippet below shows how we could record all of the fields apart from the IP address of the visitor:
Config Validation
By default, the values defined in the config file are not validated. However, the library contains a validator that can be used to ensure that your values are safe to use. To enable the config validation, you can set the following option in the config:
Custom Database Connection
By default, Short URL will use your application's default database connection. But there may be times that you'd like to use a different connection. For example, you might be building a multi-tenant application that uses a separate connection for each tenant, and you may want to store the short URLs in a central database.
To do this, you can set the connection name using the connection
config value in the config/short-url.php
file like so:
Specifying the Key Generator
By default, Short URL uses the AshAllenDesign\ShortURL\Classes\KeyGenerator
class to generate the keys for the short URL. However, you may want to use your own custom key generator class.
To do this, you can define the class to be used in the short-url.php
config file like so:
You'll just need to ensure that your custom key generator class implements the AshAllenDesign\ShortURL\Interfaces\KeyGenerator
interface.
Specifying the User Agent Parser
By default, Short URL uses whichbrowser/parser
package to parse the user agent header of the visitor when tracking visits. However, you may want to use your own custom user agent parser.
To do this, you can define the class to be used in the short-url.php
config file like so:
You'll just need to ensure that your custom user agent parser class implements the AshAllenDesign\ShortURL\Interfaces\UserAgentDriver
interface.
Specifying the Allowed URL Schemes
By default, Short URL will allow you to create a shortened URL for any URLs beginning with http://
or https://
.
However, you may want to change this list of allowed URL schemes. For example, this may be to restrict the creation to only https://
URLs. Or, it may be to allow URLs to be created using other schemes such as mailto://
or even custom schemes for your own applications.
To change the list of allowed URL schemes, you can define the list using the allowed_url_schemes
field in your short-url.php
config file like so:
Helper Methods
Visits
The ShortURL model includes a relationship (that you can use just like any other Laravel model relation) for getting the visits for a shortened URL.
To get the visits using the relationship, use or . The example snippet belows shows how:
Find by URL Key
To find the ShortURL model that corresponds to a given shortened URL key, you can use the method.
For example, to find the ShortURL model of a shortened URL that has the key , you could use the following:
Find by Destination URL
To find the ShortURL models that redirect to a given destination URL, you can use the method.
For example, to find all of the ShortURL models of shortened URLs that redirect to , you could use the following:
Tracking Enabled
To check if tracking is enabled for a short URL, you can use the method. It will return if tracking is enabled, and if not.
The following example shows how to check if a short URL has tracking enabled:
Tracked Fields
To check which fields are enabled for tracking for a short URL, you can use the method. It will return an array with the names of each field that is currently enabled for tracking.
Note: Even if the tracking options (such as ) are enabled for a short URL and returned, they won't be recorded unless the options is enabled. This can come in handy if you want to enable/disable tracking for a short URL without needing to individually set each option.
The following example shows how to get an array of all tracking-enabled fields for a short URL:
Model Factories
The package comes with model factories included for testing purposes which come in handy when generating polymorphic relationships. The ShortURL
model factory also comes with extra states that you may use when necessary, such as deactivated
and inactive
:
If you are using your own custom model factory, you can define the factories that the ShortURL
and ShortURLVisit
models should use by updating the factories
config field:
Events
Short URL Visited
Each time a short URL is visited, the following event is fired that can be listened on:
If you are redirecting users with a 301
HTTP status code, it's possible that this event will NOT be fired
if a visitor has already visited this short URL before. This is due to the fact that most browsers will cache the
intended destination URL as a 'permanent redirect' and won't actually visit the short URL first.
For better results, use the 302
HTTP status code as most browsers will treat the short URL as a 'temporary redirect'.
This means that the short URL will be visited in the browser and the event will be dispatched as expected before redirecting
to the destination URL.
Testing
To run the package's unit tests, run the following command:
Security
If you find any security related issues, please contact me directly at [email protected] to report it.
Contribution
If you wish to make any changes or improvements to the package, feel free to make a pull request.
Note: A contribution guide will be added soon.
Credits
- Ash Allen
- Jess Pickup (Logo)
- Nathan Giesbrecht
- Carlos A. Escobar
- Victor-Emil Rossil Andersen
- Julien Arcin
- Ryan Chandler
- All Contributors
Changelog
Check the CHANGELOG to get more information about the latest changes.
Upgrading
Check the UPGRADE guide to get more information on how to update this library to newer versions.
License
The MIT License (MIT). Please see License File for more information.
Support Me
If you've found this package useful, please consider buying a copy of Battle Ready Laravel to support me and my work.
Every sale makes a huge difference to me and allows me to spend more time working on open-source projects and tutorials.
To say a huge thanks, you can use the code BATTLE20 to get a 20% discount on the book.
All versions of short-url with dependencies
nesbot/carbon Version ^2.0|^3.0
illuminate/container Version ^10.0|^11.0
illuminate/database Version ^10.0|^11.0
hashids/hashids Version ^4.0|^5.0
whichbrowser/parser Version ^2.1
ashallendesign/laravel-config-validator Version ^2.6.1