Display list of routes in Laravel application

The Laravel Artisan command that I use most often is probably php artisan route:list. But it’s not always convenient to open up a shell to run the command.

Like most things in Laravel, it’s easy to set up a route in your application to display the same route details in the browser. In your app\Http\routes.php file just add the following route.

Route::get('appRoutes', function() {
    \Artisan::call('route:list');
    return "<pre>" . \Artisan::output() . "</pre>";
});

Now, when you navigate to the `appRoutes` route in your application, such as http://localhost/appRoutes, it will display the routes just like in the shell.

And if you want to put a little bit of security around this route, such as only allowing authenticated users to execute it, just reference the auth middleware like this:

Route::get('appRoutes', function() {
    \Artisan::call('route:list');
    return "<pre>" . \Artisan::output() . "</pre>";
})->middleware(['auth']);

Steps for configuring Laravel on Apache HTTP Server

While most people develop Laravel applications on the excellent Homestead platform, which uses Nginx for the HTTP server, I still prefer to use Apache 2 HTTP server, because it is most widely supported, especially on shared hosting. Accordingly, I frequently set up Linux hosts, such as Vagrant boxes, to run Laravel with Apache. Here are the key (and sometimes easy to overlook!) steps.

  1. Install PHP, Apache HTTP server, and MySQL.
  2. Enable mod_rewrite for Apache.
  3. Install Composer globally.
  4. Create your Laravel project in user directory.
  5. Set permissions for Laravel folders.
  6. Set up an Apache virtual host for your Laravel project.
  7. Add new virtual host to hosts file.
  8. Enable new virtual host and (optionally) disable the default virtual host.
  9. [Optional] Install Node.JS JavaScript engine, which is required for Laravel Elixir.
  10. [Optional] Create a MySQL (or SQLite) database to use with your Laravel project.

Let’s look at each step in detail. (Or skip to the Summary below for minimal details.)

Install PHP, Apache HTTP server, and MySQL.

To install PHP, including everything needed to support Laravel framework, on Ubuntu-based Linux system, such as Vagrant, run the following commands in the terminal/shell.

sudo apt-get update && sudo apt-get -y install software-properties-common python-software-properties
sudo apt-add-repository -y ppa:ondrej/php && sudo apt-get update
sudo apt-add-repository -y ppa:git-core/ppa && sudo apt-get update
export DEBIAN_FRONTEND=noninteractive
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password mysql_password'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password mysql_password'
sudo apt-get -y install mysql-server
sudo apt-get -y install php5.6-bz2 php5.6-cli php5.6-curl php5.6-json php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-readline php5.6-sqlite3 php5.6-xml php5.6-xsl php5.6-zip php-xdebug apache2 libapache2-mod-php5.6 git curl

By using Ondrej Sury‘s PPA for PHP, you can install PHP 5.5, 5.6, or 7.0 (and soon 7.1!) or any combination. See this article for more details. Specifically, to install PHP 7.0 instead of 5.6, just replace each instance of php5.6 with php7.0 above.

Also, note that using debconf-set-selections utility allows us to install MySQL without being prompted for the root password. In this case, we set the password to mysql_password, but you probably want to use something else.

Enable mod_rewrite for Apache.

One of the most frequently encountered difficulties when starting with Laravel on Apache HTTP server is that the mod_rewrite extension is not enabled. Laravel requires mode_rewrite to properly transform URLs to be interpreted by its routing function via .htaccess. On some Ubuntu platforms, mod_rewrite will be enabled by default, but for enhanced security, the default is now for it to be disabled. To enable
mod_rewrite extension, run:

sudo a2enmod rewrite
sudo service apache2 restart
sudo apachectl -t -D DUMP_MODULES | grep rewrite

The last command (apachectl) should return rewrite_module (shared). If this is not returned, then some failure occurred with the installation or configuration of mod_rewrite extension.

Install Composer globally.

The Laravel framework (and most recent PHP packages) use the Composer package management utility. Composer allows you to quickly and easily install the PHP modules that you need without having to worry about managing all of their dependencies. Likewise, Composer will automatically keep all of your packages updated, when maintainers publish new versions.

To simplify use of Composer, we are going to install it globally, so that you can run it from most any directory in the terminal/shell. (The Laravel installation instructions have you install Composer specifically for your Laravel project, but that’s usually not necessary.) We won’t go over all of the details for install Composer, because the Composer web site has simple, yet thorough instructions. Just go to the Composer Download page and follow the instructions. The only change to make to the given instructions is that on the third step, run the installer like this:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Since we are installing to the /usr/local/bin directory you must run this command as root user by using sudo.

The Composer installer will report the results and you should be able to verify successful installation by running composer in terminal/shell which should give you a list of all of the Composer commands.

Create your Laravel project in user directory.

Now we are ready to create our Laravel project, which installs all of the necessary files into a new directory. For this example, we will create our project in a user directory, which simplifies managing the permissions of the files and directories. Specifically, we’ll install to projects/laravel_project under our user’s home directory ($HOME). Here’s how to do it:

cd $HOME
mkdir projects
cd projects
composer create-project laravel/laravel laravel_project --prefer-dist 5.2.*

In this example, we are explicitly installing version 5.2 of Laravel framework, but you can choose any version; check this article for details. Also, we use the Composer --prefer-dist parameter to reduce the amount of data that must be downloaded.

After a few minutes, all of the necessary files will be downloaded and installed, including all of the dependencies. Composer should finish with the messages:

Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
> php artisan key:generate
Application key [base64:Q1wREnWZ5E/AmhQ8JLZr85NjiFot9IIDJ8+vTeWnNts=] set successfully.

Of course, your application key will be different. And, as shown, you can always re-generate the key by running php artisan key:generate in your project directory.

To confirm that the installation succeeded, run:

cd laravel_project
php artisan

This should give you a list of the Artisan commands. Artisan is Laravel’s powerful built-in command-line utility, which simplifies many common development tasks. Check it out!

Set permissions for Laravel folders.

Another common pitfall in configuring Laravel on Linux is setting the appropriate permissions for a couple of the directories. Laravel needs to be able to write transient (temporary) data to some directories while it’s performing it’s magic, specifically the storage and bootstrap/cache directories. Run the following commands from the Laravel base project folder (e.g., $HOME/projects/laravel_project):

chmod -R 777 storage bootstrap/cache

Essentially, this sets “full access” permissions on these directories, so that no matter what process executes the Laravel application, it will be able to read from and write to them. Read more about Linux file permissions and how to manage them here.

Set up an Apache virtual host for your Laravel project.

Probably the one thing (from the developer’s perspective anyway!) that Nginx makes so simple compared to Apache is virtual host configuration. Virtual hosts is the web server terminology for allowing a single machine (host) to serve multiple web sites. For us, the main value is in allowing us to create an abbreviated name for our Laravel project. In addition, for Laravel, we actually must use a virtual host, so that we can properly set permissions for the project directories to avoid ‘403 Forbidden’ HTTP error, due to changes to default security settings in versions 2.4.3 and above of Apache server. See this article for more details.

In this step, we’ll create a new site configuration named laravel_project.conf which contains our named virtual host configuration. All of the files that we’ll be working with are system files owned by the root user, so we must use sudo command with all of them. Also, don’t feel obligated to use the nano editor; I just use it here, since it’s installed by default on Ubuntu Linux (and it’s a pretty good editor, too!).

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/laravel.conf
sudo nano /etc/apache2/sites-available/laravel_project.conf

Modify the laravel_project.conf contents so that they look like this:

NameVirtualHost *:8080
Listen 8080

<VirtualHost *:8080>
    ServerAdmin admin@example.com
    ServerName laravel.dev
    ServerAlias www.laravel.dev
    DocumentRoot /home/user/projects/laravel_project/public
    
    <Directory /home/user/projects/laravel_project/public/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            Require all granted
    </Directory>
    
    LogLevel debug
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

In this file, replace /home/user with the name of the $HOME directory for your account on the machine. (This is the directory where you created the projects directory earlier.) Also, note that the root directory (DocumentRoot) is the public directory in your Laravel project folder. This is mainly for security as it means that the actually application files are stored in directories that the web server does not have access to. Finally, observe that we have specified that the TCP port that Apache will allow connections on (“listen” on) for the virtual host is 8080. This is a frequently used alternate port to the standard HTTP port of 80. We use 8080 to avoid conflicting with any other web applications already installed on your system.

Add new virtual host to hosts file.

You probably noticed in the previous step when setting up the virtual host configuration for our project, we specified laravel.dev as the ServerName. Basically, this is the “short” name for our virtual host. So, everything is set from the virtual host perspective to use this name. However, we need to configure our machine to recognize this name. To do this, we add an entry to machine’s hosts file. Open the hosts file:

sudo nano /etc/hosts

In the hosts file, add this new line at the end (or anywhere):

127.0.0.1   laravel.dev

Save the file.

Enable new virtual host and (optionally) disable the default virtual host.

After all of this, we are almost done! The last thing that we need to do is enable the virtual host configuration laravel_project.conf that we created above. To do this:

sudo a2ensite laravel_project.conf

You’ll be prompted to reload the Apache server process. However, before we do that, we (optionally) may want to disable the default Apache configuration. Again, if you have other web applications, such as phpMyAdmin, running on your machine, you do not want to disable this configuration. Otherwise, to avoid confusion, it probably makes sense to disable it:

sudo a2dissite 000-default.conf

Note that enabling (a2ensite) and disabling (a2dissite) don’t delete or otherwise change your configurations; they simply turn them “on” or “off”. (Basically, these utilities create or remove a symbolic link to the configurations in the /etc/apache2/sites-available directory to the same name in the /etc/apache2/sites-enabled directory.)

Now, we are ready to restart Apache server and see if everything works! To restart the server:

sudo services apache2 restart

Now, open a web browser and enter http://laravel.dev:8080/ in the address field. You should see the standard Laravel landing page screen.

If you don’t see the landing page, then check that the above steps were performed correctly. Specifically, ensure that you have set permissions on storage and bootstrap/cache directories and that you have specified Require all granted in the section of laravel_project.conf.

[Optional] Install Node.JS JavaScript engine, which is required for Laravel Elixir.

These final two steps are entirely optional, but they can be helpful in setting up a fully-functional development environment.

In this step, we install the popular Node.JS JavaScript engine. Node.JS provides many great utilities, including supporting the Gulp task-runner/build utility used to process CSS and JavaScript files, which underpins the Laravel Elixir tool.

Previously, I have written about installing Node.JS from source code. However, using the official Node.JS PPA makes things a snap. To install Node.JS, do the following:

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get -y install nodejs

The script that is downloaded via curl sets up the Node.JS repositories and updates the repository cache, so you can proceed directly to installation.

To confirm that the install succeeded, check the versions of Node.JS and its companion NPM utility:

node -v
npm -v

[Optional] Create a MySQL (or SQLite) database to use with your Laravel project.

The final (optional) step in the process is to create a MySQL database for your project. Instead of showing all the details here, please refer to this article. It includes information about setting the database configuration details in the Laravel .env file so that Laravel can load them directly.

Summary

To wrap up this article, here are the steps with only the actual commands that you run to use as a quick reference.

  1. Install PHP, Apache HTTP server, and MySQL.
  2. sudo apt-get update && sudo apt-get -y install software-properties-common python-software-properties
    sudo apt-add-repository -y ppa:ondrej/php && sudo apt-get update
    sudo apt-add-repository -y ppa:git-core/ppa && sudo apt-get update
    export DEBIAN_FRONTEND=noninteractive
    sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password mysql_password'
    sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password mysql_password'
    sudo apt-get -y install mysql-server
    sudo apt-get -y install php5.6-bz2 php5.6-cli php5.6-curl php5.6-json php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-readline php5.6-sqlite3 php5.6-xml php5.6-xsl php5.6-zip php-xdebug apache2 libapache2-mod-php5.6 git curl
    
  3. Enable mod_rewrite for Apache.
  4. sudo a2enmod rewrite
    sudo service apache2 restart
    sudo apachectl -t -D DUMP_MODULES | grep rewrite
    
  5. Install Composer globally.
  6. php -r &quot;copy('https://getcomposer.org/installer', 'composer-setup.php');&quot;
    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    php -r &quot;unlink('composer-setup.php')&quot;
    
  7. Create your Laravel project in user directory.
  8. cd $HOME
    mkdir projects
    cd projects
    composer create-project laravel/laravel laravel_project --prefer-dist 5.2.*
    ## Check installation
    cd laravel_project
    php artisan
    
  9. Set permissions for Laravel folders.
  10. chmod -R 777 storage bootstrap/cache
    
  11. Set up an Apache virtual host for your Laravel project.
  12. sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/laravel.conf
    sudo nano /etc/apache2/sites-available/laravel_project.conf
    
    ## laravel_project.conf
    NameVirtualHost *:8080
    Listen 8080
    
    <VirtualHost *:8080>
        ServerAdmin admin@example.com
        ServerName laravel.dev
        ServerAlias www.laravel.dev
        DocumentRoot /home/user/projects/laravel_project/public
        
        <Directory /home/user/projects/laravel_project/public/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
                Require all granted
        </Directory>
        
        LogLevel debug
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  13. Add new virtual host to hosts file.
  14. sudo nano /etc/hosts
    

    In the hosts file, add this new line at the end (or anywhere):

    127.0.0.1   laravel.dev
    
  15. Enable new virtual host and (optionally) disable the default virtual host.
  16. sudo a2ensite laravel_project.conf
    sudo a2dissite 000-default.conf  ### OPTIONAL
    sudo services apache2 restart
    

    Open web browser to http://laravel.dev:8080/.

  17. [Optional] Install Node.JS JavaScript engine, which is required for Laravel Elixir.
  18. curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
    sudo apt-get -y install nodejs
    node -v
    npm -v
    
  19. [Optional] Create a MySQL (or SQLite) database to use with your Laravel project.
  20. mysql -uusername -ppassword
    mysql> CREATE DATABASE `laravel` CHARACTER SET utf8 COLLATE utf8_general_ci;
    mysql> CREATE USER 'laravel_user'@'%' IDENTIFIED BY 's3cr3t';
    mysql> USE `laravel`;
    mysql> GRANT ALL PRIVILEGES ON `laravel`.* TO 'laravel_user'@'%';
    mysql> FLUSH PRIVILEGES;
    mysql> exit;
    

    Note that on the GRANT statement, the backticks (`) are only around the database name. The .*, which means “all tables”, are outside of the backticks.

    To see the permissions (privileges) granted to laravel_user, in the MySQL shell, run:

    mysql> SHOW GRANTS FOR 'laravel_user'@'%';
    

    This should return something like:

    +-----------------------------------------------------------+
    | Grants for laravel_user@%                                 |
    +-----------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'laravel_user'@'%'                  |
    | GRANT ALL PRIVILEGES ON `laravel`.* TO 'laravel_user'@'%' |
    +-----------------------------------------------------------+
    

    An alternate method of creating the database non-interactively (i.e., without logging into MySQL directly, but by running commands at the shell prompt) is to use the MySQL -e command-line parameter:

    mysql -uusername -ppassword -e "CREATE DATABASE \`laravel\` CHARACTER SET utf8 COLLATE utf8_general_ci;"
    mysql -uusername -ppassword -e "CREATE USER 'laravel_user'@'%' IDENTIFIED BY 's3cr3t';"
    mysql -uusername -ppassword -e "USE \`laravel\`; GRANT ALL PRIVILEGES ON `laravel`.* TO 'laravel_user'@'%'; FLUSH PRIVILEGES;"
    

    Note that you must “escape” the backtick characters (`) by putting a backslash (\) in front of them.

    Update the .env file in the root directory of your project with the appropriate parameter values to match your new database:

    ## .env file
    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=laravel
    DB_USERNAME=laravel_user
    DB_PASSWORD=s3cr3t
    

Hope that you found this useful. Let me know your thoughts and questions in the comments.

Install PHP 5.6 (or 5.5) in Ubuntu 16.04 LTS (Xenial Xerus)

Ubuntu has released the latest LTS (Long-Term Support) version 16.04 (Xenial Xerus) of their distribution. The official Ubuntu repositories include only PHP 7.0, since it is the official version (at the time of the 16.04 release). However, many developers still use PHP 5.5 or 5.6 in their production environments, so developing on these versions is frequently necessary. As usual, the PPAs come to our rescue for installing PHP 5.5 or 5.6 on Ubuntu 16.04. Here’s how.

First, we add the PHP 5 PPA maintained by Ondrej Sury and update the repository data on your Ubuntu installation:

sudo apt-add-repository -y ppa:ondrej/php
sudo apt-get -y update

If you are familiar with these repositories, you’ll notice that the PHP version is not included in the PPA repository name. For Ubuntu 16.04, both PHP 5.5 and 5.6 are included in the PPA. (If you need PHP 5.4 or earlier, you’ll need to stay with Ubuntu 14.04 LTS Trusty Tahr.)

Now, we can install both PHP 5.6 (or 5.5) and 7.0, along with support for Apache 2 and MySQL, including various PHP modules, such as those required for Laravel framework:

sudo apt-get -y install php7.0 php5.6-mysql php5.6-cli php5.6-curl php5.6-json php5.6-sqlite3 php5.6-mcrypt php5.6-curl php-xdebug php5.6-mbstring libapache2-mod-php5.6 libapache2-mod-php7.0 mysql-server-5.7 apache2

As with any LAMP installation on Ubuntu, you’ll be prompted for the administrator (root) password for MySQL. Also, note that the package for XDebug (php-xdebug) does not include a version, since it supports both PHP 5.6 (or 5.5) and 7.0. Most other packages are PHP version-specific. If you aren’t sure, just enter part of the name on the apt-get command line and press <Tab> to see the various options.

Once we have PHP 5.6 (or 5.5) and 7.0 installed, we can easily switch between them from the shell using these commands. To enable PHP 5.6 (and disable PHP 7.0) use this command:

sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; echo 1 | sudo update-alternatives --config php

Similarly, to switch from PHP 5.6 to PHP 7.0, use this command:

sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart ; echo 2 | sudo update-alternatives --config php

Or, better yet, why not set up a couple of simple Bash aliases in your .bashrc to take care of this for you:

alias phpv5='sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; echo 1 | sudo update-alternatives --config php'
alias phpv7='sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart ; echo 2 | sudo update-alternatives --config php'

Now, you can just run phpv5 or phpv7 to switch to PHP 5.6 or PHP 7.0, respectively.

In the commands above to switch/re-start PHP versions, the update-alternatives command is used switch the version of PHP that is used when running PHP from the shell/command line. This is especially important when using PHP command-line tools, such as Composer and the Laravel Artisan commands.

Update

If you run into problems starting Apache 2 web server (sudo service apache2 restart) with the error “Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.”, then run the following commands in Ubuntu:

sudo a2dismod mpm_event
sudo a2enmod mpm_prefork
sudo service apache2 restart

This should work for both PHP 5.6 and PHP 7.0; therefore, you should not need to run this again when switching between PHP versions.

Let me know if you have any questions in the comments below.

Error creating new Laravel project with ‘composer create-project’

After upgrading (or installing) version 1.0.0-beta1 of Composer, I found that I was unable to successfully create a new Laravel project using composer create-project command. It appeared that everything was successful, but the dependency resolution process fails with this error message (or similar) when running the command composer create-project laravel/laravel laravel "5.1.*" --prefer-dist:

 [RuntimeException]                                                                                                                                                                      
Error Output: PHP Warning:  require(/home/vagrant/laravel/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /home/vagrant/laravel/bootstrap/autoload.php on line 17                                                                                                                                                    
PHP Fatal error:  require(): Failed opening required '/home/vagrant/laravel/bootstrap/../vendor/autoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/vagrant/larave/bootstrap/autoload.php on line 17

Upon investigation, you’ll find that this line is trying to load autoload.php from the Laravel (Composer) vendor directory, but that the vendor directory does not yet exist. There is some discussion and debate on GitHub Composer project issue #5066 about whether this is a Composer problem or Laravel problem. However, it does not seem clear about whether or not there will be a fix for the issue.

In the meantime, you can work around the problem by reverting to version 1.0.0-alpha11 (or earlier) version of Composer to return the previous behavior. To do so, follow the usual instructions on the Composer site for command-line installation, except when running php composer-setup.php (or sudo php composer-setup.php on Linux), use the --version option to specify version 1.0.0-alpha11 (or earlier). For example:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer --version=1.0.0-alpha11

Subsequently, you should be able to run composer create-project to create your new project.

After you have created your project, you can upgrade to the later version of Composer, using composer self-update, but, of course, you’ll have to revert to version 1.0.0-alpha11 (or earlier) the next time you create another project. Nevertheless, for doing updates to existing projects, such as adding packages, the later version works fine.

Increase session time-out in phpMyAdmin

If you do any development with PHP and MySQL, undoubtedly, you are familiar with phpMyAdmin, which is a web-based tool for managing and editing MySQL databases. While phpMyAdmin is an excellent tool, one of it’s annoying aspects is the default session time-out of 30 minutes. However, you can make a quick configuration change to extend the time-out to most any value that you like.

To do this, just open the config.inc.php file for phpMyAdmin (which is in the /etc/phpmyadmin directory on Ubuntu Linux) in an editor as the administrative user (i.e., use sudo in Linux). Then, add these lines within the 'Servers' configuration section:

/* Set session time out to 8 hours (28800 seconds) */
$cfg['Servers'][$i]['LoginCookieValidity'] = 28800;

Save the changes and restart the Apache web server:

sudo service apache2 restart

The next time that you log into phpMyAdmin, you will stay logged in for 8 hours (or whatever duration you specified). For more information, see phpMyAdmin documentation for LoginCookieValidatity.

Install latest version of Adminer MySQL administration tool on Ubuntu Linux

Adminer is a lightweight, PHP-based MySQL administration tool that is a great alternative to PHPMyAdmin. It comes as a single file and can easily be installed globally on your Ubuntu Linux box, including a Vagrant box.

The prerequisites for installing and using Adminer are PHP, MySQL, and Apache, the so-called LAMP stack. To install them on Ubuntu, in the terminal, run:

sudo apt-get install php5 php5-cli php 5-mysql mysql-server apache2

You’ll be prompted for configuration information during the installation.

Once the LAMP stack is installed, install Adminer by running these commands in the terminal:

sudo mkdir /usr/share/adminer
sudo wget -O /usr/share/adminer/latest.php "http://www.adminer.org/latest.php"
sudo ln -s /usr/share/adminer/latest.php /usr/share/adminer/adminer.php
echo "Alias /adminer.php /usr/share/adminer/adminer.php" | sudo tee /etc/apache2/conf.d/adminer.conf
sudo service apache2 restart

After installing Adminer, open a web browser and navigate to http://localhost/adminer.php. The Adminer login screen should be displayed. You can now log in to any of the MySQL databases configured.

If you want to upgrade to a new version of Adminer later, since you created a symlink to the latest.php, you can simply run this command in the terminal:

sudo wget -O /usr/share/adminer/latest.php "http://www.adminer.org/latest.php"

Use PHP array_map() function to remove white space from array elements

One common scenario in PHP is take an input list, such as some search criteria entered by a user, and tokenize the input into individual items using a separator, such as comma. The PHP explode() function works great for splitting up the individual values into an array(). However, if have white space before or after the separator, each of the elements of the new array will contain that white space. You could use a foreach loop, with the $key => $value syntax to apply the trim() function to each item. But PHP has the perfect function already built in: array_map().

array_map() takes a callback function as its first argument and an array as the second argument. It applies the specified callback function to each element of the array that is passed in. You simply provide the name of the callback function as a string (either in single or double quotes) and the function can be a function within your code or any of the built-in PHP functions. For example, to put all of the pieces together, you could do something like this:

$input = "apricot, banana, cherry, dewberry, eggplant, fig";
$output_trimmed = array_map("trim", explode(',', $input));