Giter Site home page Giter Site logo

yii2-mini's Introduction

基于 Yii2 组件搭建 Web 项目

准备工作

新建一个项目目录。

安装 PHP 7.4 和 Composer,或使用 Docker:

docker run --rm -it -v $(pwd):/project -w /project -u $(id -u):$(id -g) modicn/php:7.4.23-fpm-alpine3.14-dev ash

运行 PHP 内置 Web 服务器

创建 public 目录作为 Web 目录(开放 Web 访问的目录),出于安全方面的考虑,Web 目录里只保存前端静态文件和 PHP 入口文件。

public 目录里创建入口文件 index.php,内容为:

<?php

echo 'Hello World!';

运行 PHP 内置的 Web 服务器,-S 参数指定监听地址,-t 参数指定 Web 目录:

php -S 0.0.0.0:8080 -t public/

访问 http://实际地址:实际端口 ,页面输出“Hello World!”。

使用 Composer 管理依赖,添加 yiisoft/yii2

Yii2 框架的核心代码包是 yiisoft/yii2官方项目模板使用的版本约束是~2.0.14

在项目目录里执行 composer require "yiisoft/yii2:~2.0.14",报错找不到 bower-asset/jquery 等依赖:

  Problem 1
    - yiisoft/yii2[2.0.14, ..., 2.0.15.1] require bower-asset/jquery 3.2.*@stable | 3.1.*@stable | 2.2.*@stable | 2.1.*@stable | 1.11.*@stable | 1.12.*@stable -> could not be found in any version, but the follow
ing packages provide it:
      - craftcms/cms Craft CMS
      - yidas/yii2-bower-asset Bower Assets for Yii 2 app provided via Composer repository
      - yidas/yii2-composer-bower-skip A Composer package that allows you to install or update Yii2 without Bower-Asset
      ...

原因是,bower-asset/*npm-asset/* 等前端包,在 Composer 的默认仓库里不存在。Yii2 官方项目模板的解决办法是添加 https://asset-packagist.org 仓库:https://github.com/yiisoft/yii2-app-advanced/blob/2.0.44/composer.json#L39

出于前后端分离的考虑,不应使用 Composer 管理前端依赖,所以创建 composer.json 文件,使用 replace 配置来跳过前端包的安装,具体内容如下:

{
    "replace": {
        "bower-asset/inputmask": "*",
        "bower-asset/jquery": "*",
        "bower-asset/punycode": "*",
        "bower-asset/yii2-pjax": "*",
        "npm-asset/bootstrap": "*"
    }
}

再执行 composer require "yiisoft/yii2:~2.0.14",可以成功安装依赖。

Yii2 Web 项目的最小代码

Yii2 Web 项目的各组件,是由 yii\web\Application 类集成到一起的,其有两个必须的参数:idbasePath。官方文档:https://www.yiiframework.com/doc/guide/2.0/en/structure-applications#required-properties

controllerNamespace 参数可以用来指定 Controller 类所在的命名空间。

mini\controllers\DemoControlleractionIndex() 对应的路由是 demo/index,可以通过 http://实际地址:实际端口/index.php?r=demo%2Findex 访问。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.