PHP 開發起步示例
阿新 • • 發佈:2022-05-28
PHP 開發起步示例
開發y一個demo
-
用 phpstorm 建立新專案 demo
-
構建開發程式設計環境
- 使用 psr-4 標準化載入機制
- 使用 composer 構建 初始化 專案
- 專案目錄下開啟命令列執行
composer require symfony/var-dumper
引入 composer.json 的初始結構
-
專案結構顯示
> tree -L 2 demo demo ├── composer.json ├── composer.lock └── vendor ├── autoload.php ├── composer └── symfony
-
修改 composer.json 配置檔案,配置自動載入標準,指定
psr-4
, 並建立一個目錄,配置資訊完成對映, composer.json 檔案內容如下{ "autoload": { "psr-4": { "App\\": "app" } }, "require": { "symfony/var-dumper": "^3.4" } }
終端執行命令,讓自動載入在專案中生效
composer dump-autoload -o
輸出:Generating optimized autoload files -
編輯 index.php
<?php require_once 'vendor/autoload.php'; dump("this is a composer construct project");
執行
php index.php
輸出this is a composer construct project
-
測試自動載入,目錄結構
app ├── Helper.php └── task └── BackTask.php
編輯 index.php
<?php require_once 'vendor/autoload.php'; dump("this is a composer construct project"); \App\Helper::println("static function output"); $helper = new \App\Helper(); $helper->sayHello("object function output"); $backTask = new \App\Task\BackTask();
執行
php index.php
輸出正常
遇到問題記錄
問題1:
$ composer require symfony/var-dumper
The "https://packagist.phpcomposer.com/packages.json" file could not be downloaded: SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed
Failed to enable crypto
failed to open stream: operation failed
https://packagist.phpcomposer.com could not be fully loaded, package information was loaded from the local cache and may be out of date
[InvalidArgumentException]
Could not find a matching version of package symfony/var-dumps. Check the package spelling, your version constraint and that
the package is available in a stability which matches your minimum-stability (stable).
require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] [<packages>]...
原因:證書失效
解決:I solved the problem with the SSL error by adding the SSL certificate
// download a certificate
wget http://curl.haxx.se/ca/cacert.pem
配置 php.ini 中的 openssl.cafile=
為 下載的檔案的 全路徑
refer: https://stackoverflow.com/questions/27206719/composer-update-fails-while-updating-from-packagist
問題2:
$ composer require symfony/var-dumper
Using version ^3.4 for symfony/var-dumper
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
- Installing symfony/polyfill-mbstring (v1.19.0): Downloading (connecting...) Failed to download symfony/polyfill-mbstring from dist: The 'https://api.github.com/repos/symfony/polyfill-mbstring/zipball/b5f7b932ee6fa802fc792eabd77c4c88084517ce' URL could not be accessed: HTTP/1.1 400 Bad Request
Now trying to download from source
- Installing symfony/polyfill-mbstring (v1.19.0): Cloning b5f7b932ee
[RuntimeException]
Failed to clone https://github.com/symfony/polyfill-mbstring.git via https, ssh protocols, aborting.
- https://github.com/symfony/polyfill-mbstring.git
Cloning into '/Users/user00/Documents/workspace/phptest/demo1/vendor/symfony/polyfill-mbstring'...
fatal: unable to access 'https://github.com/symfony/polyfill-mbstring.git/': LibreSSL SSL_connect: SSL_ERROR_SYSCALL in con
nection to github.com:443
- [email protected]:symfony/polyfill-mbstring.git
Cloning into '/Users/user00/Documents/workspace/phptest/demo1/vendor/symfony/polyfill-mbstring'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] [<packages>]...
原因:composer 源不可用
解決:配置國內的源
# 配置全域性
composer config -g repo.packagist composer https://mirrors.cloud.tencent.com/composer
# 取消全域性配置
composer config -g --unset repos.packagist
常見國內的源
composer https://packagist.org
phpcomposer https://packagist.phpcomposer.com
aliyun https://mirrors.aliyun.com/composer
tencent https://mirrors.cloud.tencent.com/composer
huawei https://mirrors.huaweicloud.com/repository/php
laravel-china https://packagist.laravel-china.org
cnpkg https://php.cnpkg.org
sjtug https://packagist.mirrors.sjtug.sjtu.edu.cn
refer: https://learnku.com/articles/15977/composer-accelerate-and-modify-mirror-source-in-china