Hướng dẫn dùng last debian trong PHP
Sử dụng tùy chọn
# tar -cvf filename.tar file1 folder1 --totals Total bytes written: 20561920 (20MiB, 354MiB/s)2 để giải nén ở chế độ yên lặng – quiet, không hiển thị thông tin gì trong quá trình giải nén. Show Composer is a dependency manager made for the PHP programming language. It allows users to easily manage and integrate external dependencies and libraries for PHP software development. This makes the software development process much more streamlined and efficient. With already pre-set dependencies and libraries, users no longer need to start their projects from scratch. In this tutorial, we will show you how to install Composer on your shared hosting account, Linux, or macOS systems. We will also provide instructions on how to download, install and use a Composer package.
Installing ComposerThis section will show you how to install Composer on shared or cloud hosting accounts and such operating systems as Linux, macOS, and Windows. Important! Composer comes pre-installed on Hostinger’s Premium and Business shared hosting plans as well as on cloud hosting plans. If you are already using our services, skip the following part. However, if you feel your version of Composer is outdated and the latest version is needed, you may proceed with the local installation process. 1. Installing Composer on Shared Hosting, Linux, or macOSThe commands to install Composer on a shared hosting account, Linux (PC or server-based system), and macOS are the same. Pro TipHostinger provides its users with two Composer versions. If you need Composer version 1.10, use the composer command. Follow these steps to install Composer on your system:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" If you are using macOS or Linux, ensure that you have PHP installed beforehand.
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" The installer’s signature is the long string of characters as shown in the example command above (’55ce33d…’). It changes every time a new version of Composer comes out. Therefore, fetch the latest SHA-384 command from the Composer download page.
Meanwhile, the global installation allows you to run Composer from anywhere on your system by storing it in the /usr/local/bin directory. However, the global installation method won’t work on shared and cloud hosting. Local Installation php composer-setup.php Global Installation php composer-setup.php --install-dir=/usr/local/bin --filename=composer You should get the following result: All settings correct for using Composer Downloading… Composer (version 2.4.2) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer
php -r "unlink('composer-setup.php');"
composer The command line will return the following output: ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ / __ `__ / __ / __ / ___/ _ / ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / ____/____/_/ /_/ /_/ .___/____/____/___/_/ /_/ Composer version 2.4.2 2022-09-14 16:11:15 Important! If you installed Composer locally, you need to use php composer.phar command within the same directory where it was installed in order to use Composer. 2. Installing Composer on WindowsGetting started with Composer on a Windows machine is a bit different. No command-line instructions are necessary to download and install the software. Simply follow these steps:
composer Command-line will return the following result: ______ / ____/___ ____ ___ ____ ____ ________ _____ / / / __ / __ `__ / __ / __ / ___/ _ / ___/ / /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ / ____/____/_/ /_/ /_/ .___/____/____/___/_/ /_/ Composer version 2.4.2 2022-09-14 16:11:15 You now have Composer installed on your Windows system. Open the Command Prompt and run it from any place. Generating and Understanding composer.jsonNow, the exciting part – using Composer in your PHP project. To achieve this, you need to generate a composer.json file. This file contains packages (dependencies) that should be downloaded. Furthermore, composer.json also checks for version compatibility for your project. If you use an older package, composer.json will let you know that one may avoid future issues. You have the option to create and update composer.json yourself. However, the best practice is to let Composer do this for you. Thus we don’t recommend creating the file manually. Let’s demonstrate the practicality of composer.json by creating a sample project. Our project is a simple PHP timer, allowing developers to determine how much time a code takes to execute. This is highly useful for debugging and optimization purposes. Follow these steps for more information:
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"0
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"1
As you can see, several timer packages are available, and each has a name and a short description. For our example, we will pick phpunit/php-timer as it has the most downloads and GitHub Stars.
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"2 The output of the terminal will show the version of phpunit/php-timer: php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"3 In our case, the package update range is >=5.0.3 <6.0.0, as version 6.0.0 will break the backward compatibility. For detailed information about versioning in Composer, visit the official documentation page. After executing the previous command, your project directory will have two new files (composer.json and composer.lock) and a folder named vendor. This is a specific directory where Composer stores all your packages and dependencies. Using Autoload ScriptYour project is almost ready. The only thing left is loading the dependency into your PHP script. Fortunately, Composer’s autoload file helps you to complete this process faster. To use autoloading, write the following line before you declare or instantiate new variables in your script: php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"4 We’ll give you an example to help you understand this concept better. Let’s say we want to test our phptimer project. Open the nano text editor to create a script named demo.php: php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"5 Paste the following to the file: php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"6 Now, run the following script: php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"7 Finally, the terminal will display an output similar to the following: php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"8 Updating Your Project DependenciesYou have to know how to update your packages, and this can be done in one of two ways:
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"9
php composer-setup.php0 Remember to replace the vendor/package value with the name of the package you want to update. Composer will also update the composer.json and composer.lock files by running the update command to match the current state of your project dependencies. ConclusionComposer helps developers manage the dependencies and libraries of their PHP projects. It allows developers to integrate and manage open-source packages from a single place easily. This article taught you how to install and use Composer effectively. To summarize, let’s review all the steps once again:
We hope that you found this tutorial helpful. If you have questions or further insights, please leave them in the comments section. The author Ignas R. Ignas is a Technical Content Writer at Hostinger. After helping people tackle even the most complex technical issues as a Customer Success Specialist, his current goal is to write easy-to-follow articles so that these issues will not happen at all. During his free time, Ignas likes to play video games and fix up things around his house. |