Some useful composer options we are use for production Docker setup in Laravel apps

Composer logo

As a part of our Docker production setup of Laravel web application we are using the following composer install with some options described below:

  • –optimize-autoloader or -o: This flag tells Composer to optimize the autoloader for better performance. Autoloading is a mechanism in PHP that automatically loads classes when they are needed. Optimizing the autoloader can make your application load faster by reducing the number of file operations required to load classes.
  • –no-dev: This flag tells Composer not to install the development dependencies specified in the `composer.json` file. Development dependencies typically include things like testing libraries, code analysis tools, and other packages that are not needed in a production environment.
  • –prefer-dist: This flag tells Composer to prefer installing package distributions (i.e., ZIP or TAR archives) over the source code from version control repositories. Using distributions can be faster and more efficient because it avoids the need to compile or build the packages from source.

So, when you run

composer install --optimize-autoloader --no-dev

, you are installing the project’s production dependencies while optimizing the autoloading process for better performance and excluding any development-specific dependencies. This is commonly used when preparing a PHP project for deployment to a production server.

We hope this information will help you to prepare your Laravel application for production environment.

The full list of all possible options you could check at composer documentation page.

How to install TeamViewer on Ubuntu Linux 22.04

To install TeamViewer on Ubuntu Linux 22.04 use the follwoing commands:

sudo su

This command will make you Linux administrator super user root. It is needed to install or update any system software on your system.

The first thing we need is to install SSL certificates using commands below:

cd /tmp
wget https://download.teamviewer.com/download/linux/signature/TeamViewer2017.asc
sudo apt-key add TeamViewer2017.asc
sudo sh -c 'echo "deb http://linux.teamviewer.com/deb stable main" >> /etc/apt/sources.list.d/teamviewer.list'

The second part of commands updates system software and install TeamViewer program itself:

sudo apt update
sudo apt upgrade

sudo apt install teamviewer

Check out the video instructions on how to install the TeamViewer on Ubuntu Linux.

PHP – swap two variable values

For example, you have a function which takes two parameters int|float $min and int|float $max parameters and you need to swap variable values vise versa, in case if $max value is less then $min value.

You can do it using the following code below:

if ($min > $max) {
    list($min, $max) = [$max, $min];
}

Git – copy few branches from old repository to a new one

Sometimes, then you need to transfer few Git branches from existing repository to a new one you could run the following commands:

git clone --no-checkout [New Git Repository URL] new-repo-folder

This command will create a new folder new-repo-folder with a .git folder inside, but without any files from branches.

Change to the repository folder:

cd new-repo-folder

Adding the necessary branches: Now you need to do “remote tracking” for the desired branches. You could do it using commands below:

git remote add old-origin [Original Git Repository URL]

git fetch old-origin dev:dev
git fetch old-origin stage:stage
git fetch old-origin prod:prod

This will add the dev, stage, and prod branches from the original repository to your local copy.

Check: Verify that you now have only these three branches.

git branch -l

In case if you have also have copied Git tags use the command below:

git tag -l

to list all Git tags.

To remove all local tags use the command below:

git tag -l | xargs git tag -d

To push all branches to remote use the following command:

git push origin --all

Join the PHP Dinos Telegram Channel!

Are you a PHP enthusiast, developer, or just curious about the world of PHP? Look no further! We’re thrilled to introduce the brand-new Telegram channel, PHP Dinos.

What to expect:

  • PHP tips and tricks.
  • PHP news and updates.
  • Engaging in discussions on PHP development.
  • Valuable resources for PHP beginners and experts alike.
  • Community support and collaboration opportunities.

Whether you’re a seasoned PHP developer or just starting your PHP journey, PHP Dinos is your go-to destination for all things PHP-related. Let’s come together to explore, learn, and share our passion for PHP programming.
Join us now at https://t.me/phpdinos and be part of a vibrant community of PHP enthusiasts. Don’t miss out on the fun – see you there!

#PHP #WebDevelopment #Programming #Community #PHPDinos

Bulk files rename shell script for Linux

If you need to make a bulk files rename on Linux you could use the following Bash Shell script:

#!/bin/bash

# Specify the directory where the files are located
directory="/home/user/Videos/2023-08-18"

# Change to the directory
cd "$directory"

# Iterate over the files matching the pattern and rename them
for file in [0-9]*.mp4; do
    new_filename=$(echo "$file" | sed -E "s/^([0-9]+)\.mp4$/_name-prefix-\1.mp4/")
    mv -v "$file" "$new_filename"
done

Just replace directory variable value to an actual path to folder with *.mp4 files on your system.

Replace _name-prefix- with a desired file names prefix.

You could use the following Gist: with bulk mp4 files rename shell script.

Check out Video tutorial on how to Bulk files rename in a bash shell script for Linux.

Generate .htaccess config file using tee shell command

If you are using Docker setup for Yii2 Advanced application template to build REST API as I do, you could be interested in the command below:

tee /app/api/web/.htaccess <<EOF
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php
EOF

it is generating .htaccess configuration file for Apache web server to redirect all requests from not existing files or directories to index.php file in API web application inside Docker container.

Multiple lines below starting < and closing EOF are the configuration file content. Such multiple lines syntax is called heredoc. You could choose any other identifier (one word in upper case letters) instead of EOF if you want.

Web server Apache Mod_rewrite module should be installed and enabled to make it work.