远程开发Laravel:PhpStorm替代VS Code挂载方案及artisan命令处理
Hey there! Let's break down how to get PhpStorm set up for your remote Laravel project, plus handle those php artisan make:* commands and keep your local/remote files in sync without headaches.
PhpStorm has a built-in Deployment tool that replaces the "mount remote disk" workflow you used in VS Code. Here's how to set it up:
- Open PhpStorm, go to Settings/Preferences (Mac:
PhpStorm > Preferences; Windows:File > Settings). - Navigate to Build, Execution, Deployment > Deployment.
- Click the
+icon, select SFTP (the standard for SSH-based remote access), and name your configuration (e.g., "My Laravel Server"). - In the Connection tab:
- Enter your server's Host, Port (default 22), Username, and either your Password or select an SSH key (keys are more secure, so go with that if you can).
- Click Test Connection to make sure PhpStorm can reach your server.
- Switch to the Mappings tab:
- Local path: Choose the folder on your Mac where you'll store a local copy of the project (e.g.,
~/Projects/MyLaravelApp). - Deployment path on server: Enter the absolute path to your Laravel project's root on the server (e.g.,
/var/www/my-laravel-project). - Web path: If this is a web app, enter the corresponding URL path (e.g.,
/).
- Local path: Choose the folder on your Mac where you'll store a local copy of the project (e.g.,
- Finally, set this configuration as your Default (use the dropdown in the Deployment settings top-right) so PhpStorm uses it automatically.
Enable Auto-Sync (Optional but Handy)
To avoid manually uploading/downloading files every time:
- Go back to Deployment settings, open the Options tab.
- Check Upload changed files automatically to the default server, then choose either:
On explicit save action: Syncs when you hitCmd+S(more controlled, recommended).On frame deactivation: Syncs when you switch away from PhpStorm.
php artisan make:* Commands You've got two solid options here, depending on your setup:
Option 1: Run Artisan Locally (Recommended)
If you have Laravel set up locally (composer dependencies installed, a basic .env file), this is the smoothest workflow:
- Open PhpStorm's built-in Terminal (bottom toolbar) and run commands like
php artisan make:controller Admin/UserControllerorphp artisan make:model Post -m. - The generated files will land in your local project folder.
- With auto-sync enabled, saving the files will automatically upload them to the remote server. Done.
Option 2: Run Artisan Remotely, Then Pull Files to Local
If you don't have a local Laravel environment set up, or need to run commands directly on the server:
- Open an SSH session in PhpStorm: Go to Tools > Start SSH Session, select your server, and log in.
cdto your project's root directory, then run your artisan command (e.g.,php artisan make:middleware CheckAdmin).- To pull the new files to your local machine:
- Right-click the project root in PhpStorm's Project view, select Deployment > Download from [Your Server Name].
- Or, if you only need specific files/folders, right-click them and choose the same download option.
If remote files get modified (via artisan commands, other developers, etc.), here's how to sync and merge cleanly:
- First, commit your local changes to Git: This ensures you have a safe backup before merging remote changes.
- Download remote changes: Use PhpStorm's Deployment tool to pull all updated files to your local project. PhpStorm will flag any conflicting files (marked with a red icon).
- Resolve conflicts:
- Double-click a conflicting file to open it. You'll see PhpStorm's conflict markers (
<<<<<<<,=======,>>>>>>>) separating local and remote changes. - Click the Resolve Conflicts button in the top toolbar, or right-click the file and select Git > Resolve Conflicts.
- Use PhpStorm's merge tool to choose which changes to keep (local, remote, or a mix), then save the file.
- Double-click a conflicting file to open it. You'll see PhpStorm's conflict markers (
- Sync back to remote: Once conflicts are resolved, commit the merged changes to Git, then upload the updated files back to the server via Deployment.
Pro Tip: Use Git as Your Primary Sync Tool
For long-term sanity, link both your local project and the remote server's project to the same Git repository. This way:
- When you run artisan commands remotely, commit the new files to Git on the server, then pull them locally with
git pull. - Conflicts are handled via Git's built-in merge tools, which are more robust than manual file sync.
- PhpStorm's Deployment can act as a backup sync, but Git should be your source of truth.
内容的提问来源于stack exchange,提问作者beccoblu

