PHP 开发起步示例


PHP 开发起步示例

开发y一个demo

  1. 用 phpstorm 创建新项目 demo

  2. 构建开发编程环境

    1. 使用 psr-4 标准化加载机制
    2. 使用 composer 构建 初始化 项目
    3. 项目目录下打开命令行运行 composer require symfony/var-dumper 引入 composer.json 的初始结构
  3. 项目结构显示

    > tree -L 2 demo
    demo
    ├── composer.json
    ├── composer.lock
    └── vendor
        ├── autoload.php
        ├── composer
        └── symfony
    
  4. 修改 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

  5. 编辑 index.php

    <?php
        
    require_once 'vendor/autoload.php';
        
    dump("this is a composer construct project");
    

    运行 php index.php 输出 this is a composer construct project

  6. 测试自动加载,目录结构

    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
  - git@github.com:symfony/polyfill-mbstring.git
    Cloning into '/Users/user00/Documents/workspace/phptest/demo1/vendor/symfony/polyfill-mbstring'...
    git@github.com: 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

原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/272542.html

(0)
上一篇 2022年7月9日
下一篇 2022年7月9日

相关推荐

发表回复

登录后才能评论