Home > Backend Development > PHP Tutorial > The Android Elephpant - Laravel on your Android Phone?

The Android Elephpant - Laravel on your Android Phone?

Christopher Nolan
Release: 2025-02-10 08:56:11
Original
973 people have browsed it

Building a PHP development environment using Termux on Android devices: A mobile development guide

The Android Elephpant - Laravel on your Android Phone?

Core points

  • Use powerful terminal emulator and Linux package collection Termux to build a PHP development environment on Android devices.
  • Running Laravel on Android requires installing packages such as PHP, Git, and Composer, and verifying the PHP installation using a simple phpinfo() test.
  • Data persistence of Android devices can be achieved through SQLite, a lightweight serverless file-type database engine, which is ideal for storing small amounts of data.
  • While Android devices cannot run complex test suites or MySQL, this setup is useful for small development tasks or emergency fixes, allowing PHP development anytime, anywhere without having to carry a laptop.

Not long ago, Christopher Pitt wrote an excellent article on how to write and run PHP code on iPad. After reading it, I thought to myself, “It’s cool to do the same thing on Android”, for example, you can write and edit code anytime on the go without having to carry your laptop with you. So I decided to do some research and see what I could come up with.

The Android Elephpant - Laravel on your Android Phone?

This tutorial is suitable for any type of Android device. I did this on my phone, but an Android tablet with a Bluetooth keyboard might be the ideal setup.

There are some different shell applications on Android. This tutorial will use an application called Termux.

The Android Elephpant - Laravel on your Android Phone?

Termux combines powerful terminal emulation and an extensive collection of Linux packages. It is also completely free and easy to use.

After installing Termux from the Play Store, first run the apt update command. According to the documentation: "This command needs to be run immediately after installation and then run regularly to receive updates."

Now is the exciting part. The first two commands I want to discuss are the apt list and apt list --installed commands. The first command will list all packages available to Termux. We can see that it supports many different programming languages, text editors, and has some useful utility packages such as zip, tar, and so on. The second command will list all installed packages. We can see that Termux has pre-installed some packages such as apt and bash.

My goal when testing Termux was to see if I could assemble a suitable *PHP development environment, so I first installed a text editor. I prefer Vim, but there are some other options available, such as Emacs and Nano. Vim's learning curve is a bit steep, but once you get the basics of it, it becomes very comfortable. You can use the apt install vim command to get Vim.

If you want to learn more about vim, here is a very good article, or, after installation, type vimtutor to use the built-in tutorial.

If you test this on an Android phone, running vim will bring the first set of problems. How do I press the Escape key? Termux has a large list of shortcut keys that can be used to emulate unavailable buttons on Android keyboard:

命令
Volume Up E Escape键
Volume Up T Tab键
Volume Up 1 F1 (Volume Up 2 → F2,以此类推)
Volume Up 0 F10
Volume Up B Alt B (使用readline时向后一个单词)
Volume Up F Alt F (使用readline时向前一个单词)
Volume Up X Alt X
Volume Up W 上箭头键
Volume Up A 左箭头键
Volume Up S 下箭头键
Volume Up D 右箭头键
Volume Up L | (管道字符)
Volume Up U _ (下划线)
Volume Up P Page Up
Volume Up N Page Down
Volume Up . Ctrl (SIGQUIT)
Volume Up V 显示音量控制

Now that our editor is up and running, it's time to install the packages we need: PHP, Git, and Composer.

apt install php
apt install git
Copy after login
Copy after login

This will install the latest PHP and Git packages.

The Android Elephpant - Laravel on your Android Phone?

For Composer, we need to do some extra work. We need to go to the Composer download page and use the command line installation instructions:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Copy after login
Copy after login

This will download the installer, verify it, run it and delete it. If all goes well, we should be able to run Composer from Termux.

The Android Elephpant - Laravel on your Android Phone?

Now that we have all the tools installed, we should test if our PHP installation is running correctly. To do this, let's do a simple phpinfo() test. Let's create a new folder and test our PHP installation.

mkdir test
cd test
echo "<?php phpinfo();" ?> > index.php
php -S localhost:8080
Copy after login
Copy after login

This will create a new folder, and then create a phpinfo() file containing the index.php command. I echo it directly into the file, but you can do it with Vim. Finally, we use a PHP server to provide it to our localhost. When accessing localhost:8080 in the browser, we should see something like this:

The Android Elephpant - Laravel on your Android Phone?

We now have Composer for dependency management and git for version control. But I know what you're thinking: "We just did a simple phpinfo test, what about the rest?"

Can we install Laravel on our Android device?

At this point, we have everything we need to install and run Laravel on our Android device. To create a new Laravel project, we need to run the following command:

php composer.phar create-project --prefer-dist laravel/laravel new_project
Copy after login
Copy after login

This will create a new Laravel project in the new_project folder. The --prefer-dist option is well documented here. It may take a while to install. Once done, we can use Laravel's own Artisan command line interface to run our newly created project. In the new_project folder, we can run the following command:

php artisan serve
Copy after login
Copy after login

Accessing localhost:8000 URL in your browser should now display Laravel's homepage.

The Android Elephpant - Laravel on your Android Phone?

Success! Our Laravel installation is complete. We have successfully installed the tools we need to write and execute code. However, no development environment is complete without a method to persist data.

For Android devices, memory and storage capacity are practical issues in most cases. Therefore, Termux only provides SQLite as a way to persist data. SQLite is a serverless file-type database engine. It's lightweight and is perfect for a small amount of data, as you've read here and in this article that goes beyond the basics. First, we need to install it.

apt install php
apt install git
Copy after login
Copy after login

Next, we need to configure our Laravel project to use SQLite. In the root directory of the project, we have a .env file. This is the environment configuration file, the first file we need to edit. Use the editor of your choice to edit the following lines:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Copy after login
Copy after login

Then go to the config/database.php file and turn the following line from:

mkdir test
cd test
echo "<?php phpinfo();" ?> > index.php
php -S localhost:8080
Copy after login
Copy after login

Change to:

php composer.phar create-project --prefer-dist laravel/laravel new_project
Copy after login
Copy after login

This will make SQLite the default connection in the connections array. Make sure the database.sqlite file path is correctly pointing to your database file.

database.sqliteThe file does not exist yet, so we need to create it:

php artisan serve
Copy after login
Copy after login

This is all the configuration we need to tell Laravel to use SQLite - we can test it now. We will use Laravel's pre-built authentication system. To create a scaffold, we need to run the following command:

apt install sqlite
Copy after login
After that, we will run the migration to build our database schema. This will create the

and users tables. password_reset

<code>DB_CONNECTION=sqlite
DB_DATABASE=homestead  # 或者更改为你的数据库名称</code>
Copy after login
If we run

again, we will see that we have the option to register and log in now. Our authentication CRUD has been successfully created! php artisan serve

The Android Elephpant - Laravel on your Android Phone?

Conclusion

I did all this on my Android phone. This setup is great for small development tasks, as it has all the tools you need to start developing on smaller devices without having to carry your laptop with you.

While it is not the pinnacle of productivity, it comes in handy when it comes to an emergency repair or if you want to see how much PHP performance you can squeeze from your Android device.

Try it and tell us your thoughts, if you build something interesting with PHP on Android, please market to us your concept and we will write about it!


*"Appropriate" refers to the appropriateness for the context. Android phones don't run MySQL, and they can't run complex test suites, especially end-to-end tests, but some other things may run well enough to do some work.

(The FAQs part is omitted because it does not match the pseudo-original goal and is too long.)

The above is the detailed content of The Android Elephpant - Laravel on your Android Phone?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template