The Android Elephpant - Laravel on your Android Phone?
Building a PHP development environment using Termux on Android devices: A mobile development guide
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.
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.
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
This will install the latest PHP and Git packages.
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');"
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.
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
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:
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
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
Accessing localhost:8000 URL in your browser should now display Laravel's homepage.
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
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');"
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
Change to:
php composer.phar create-project --prefer-dist laravel/laravel new_project
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.sqlite
The file does not exist yet, so we need to create it:
php artisan serve
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
and users
tables. password_reset
<code>DB_CONNECTION=sqlite DB_DATABASE=homestead # 或者更改为你的数据库名称</code>
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
