Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Articles Tagged ‘laravel’

Using session data in Laravel

Tuesday, November 24th, 2015 by Servage
session-dataThe session is a system whereby your server can store and retrieve information for a given user. Technically it works by generating a unique session id for each new session and whatever is stored in the session is then actually stored on the server. The user's browser receives a cookie with the session id, and thereby the server knows which user belongs to which session data. Note how the European Union has imposed regulations on how website owners are required to inform users about the usage of cookies. This comes as a reaction to major data aggregation and privacy discussions with impact on almost any website owner. At least anyone who uses ...

Pagination with Laravel

Tuesday, November 17th, 2015 by Servage
pagination-tableIn a recent article the concept behind pagination was described. It is a useful technique to overcome information overload and can fortunately be easily implemented into existing projects. Some frameworks actually have pagination right in their core functionality. In this article it will be demonstrated using Laravel. Controller pagination example The example below fetches all products from the database with respect to the current pagination settings. The paginate command states that 10 items should be fetched per page, while the page number would come from URL parameters. class ProductsController extends Controller { /** * Show all of the products * * ...

Sending emails in Laravel

Sunday, October 18th, 2015 by Servage
emailThe ability to send emails from within your web application is essential to almost any website or system. This rather trivial task is sometimes complicated by relatively unreliable libraries and unstable sending methods. If you have been around PHP for a while, you will probably remember the old days using the native "mail" function in PHP with Sendmail. Fortunately a lot has happened over the recent years, and great options for easy email sending (and receiving) have been well adopted in most frameworks. Laravel includes an email sending API built upon the common and very popular Swift Mailer library. It provides all the basic sending capabilities and is wrapped into a nice object ...

Working with Laravel localization

Monday, October 5th, 2015 by Servage
LocalizationWorking with websites in different languages requires a system that can handle content to be displayed depending on a language setting. This setting is usually derived from the user's profile settings, the client's browser preference, or maybe the remote IP address country. Either way, as soon as you handle more than one language, you need to deal with a dynamic display. Actually it pays of to implement localization even for just a single language, because the overhead by implementing localization is usually relatively small. However, implementing localization afterwards can be a major issue, because all the code needs to be revisited. It is much faster to implement it from the beginning. Laravel ...

Using files and storage engines with Laravel

Sunday, September 20th, 2015 by Servage
storage-enginesWhen working with PHP projects you sooner or later need to interact with files. This can be done in a variety of ways, and PHP offers a few good built-in features like the "file_get_contents" and "file_put_content" functions. However, you may need more advanced features - and may need to interact with other file systems than your local harddrive. For that purpose the Laravel framework comes with built in storage handling which is compatible with a variety of systems. It is built upon the popular library Flysystem and therefore integrates with lots of services via the same common interface. Storage configuration resides in the "config/filesystems.php" file. You can add multiple engines such as ...

Event based development in Laravel

Friday, September 4th, 2015 by Servage
event-processWorking with events is a great way of handling actions in response to other actions. For example if a user makes a purchase, you want to store that purchase in your database, send a confirmation email and notify sales personel to handle the order. The order would be stored in the database right away, and the email is usually something your send right away or add to an email queue. Using a queue is better than sending immediately, because the website does not have to wait for the mail to send, but that is the topic for another article. Adding the notification to sales would also be something stored in the ...

Working with logging and errors in Laravel

Sunday, August 23rd, 2015 by Servage
oops-errorLogging and error-handling is built right into Laravel 5, so you do not need to build own exception handlers and logging mechanisms as with own custom projects. This is one of the many benefits of using a ready-to-go framework like Laravel 5. Configuring Laravel logging You can set various levels of logging, and choose which grouping you want in your log files. You can change the logging related values in the "config/app.php" file. The "debug" paramenter is set to the environment value APP_DEBUG by default, so you can set it to a general value in the config file, or change locally in the environment file. Note that debugging should always ...

Useful Laravel array utility functions

Friday, August 14th, 2015 by Servage
laravelLaravel includes many useful functions. This article contains a list of great utilities for handling arrays. The function "array_add" inserts a key with a corresponding value to a given array if the key is not already present in the array. $values = ['name' => 'John']; $array = array_add( $values, 'age', 35 ); // Value of $array: // ['name' => 'John', 'age' => 35] The function "array_divide" returns two arrays with the keys and values of the original array respectively. list($keys, $values) = array_divide(['name' => 'John']); // Output values: // $keys: ['name'] // $values: ['John'] The function "array_dot" turns a multi-dimensional array into a flattened one-dimensional array using dot-notation to indicate the old depth: $array = array_dot(['foo' => ['bar' => 'baz']]); // Value of array: // ...

Handling model data in Laravel collections

Wednesday, August 5th, 2015 by Servage
collectionThere is a fluent collection wrapper provided in Laravel which makes handling many models very efficient. A collection is basically a list of models, like an array, which can be processed using various methods. The collection itself is an object which provides a range of utilities, however, it also implements array access, so you can perform array operations on it. This makes it especially powerful. It is very easy to get a collection, because you get it by default from the Eloquent model database query methods. For example if you retrieve all users using the standard methods for such a database selection, you will get a collection of user model instances. Creating a collection from ...

Using the cache engine in Laravel for better performance

Sunday, July 19th, 2015 by Servage
cache-redisOne of the common problems with website performance is slow load times due to long response times from the server. The response can be slowed down for various reasons, and thankfully there are also multiple points where optimization can occur. Regardless of network issues on either end of the connection, you can always optimize load times with caching. This may even be a significant improvement, especially when caching database operations. Laravel cache engine In the Laravel framework caching is built into the core and is therefore available in the default installation. You can basically cache anything right from your PHP code. This is useful for caching both direct database results, but also processed ...