first commit

This commit is contained in:
2026-01-25 18:18:09 +08:00
commit 509312e604
8136 changed files with 2349298 additions and 0 deletions

View File

@ -0,0 +1,31 @@
Twitter Bootstrap Extension for Yii 2
=====================================
The extension includes support for the [Bootstrap 5](https://getbootstrap.com/) markup and components framework
(also known as "Twitter Bootstrap"). Bootstrap is an excellent, responsive framework that can greatly speed up the
client-side of your development process.
The core of Bootstrap is represented by two parts:
- CSS basics, such as a grid layout system, typography, helper classes, and responsive utilities.
- Ready to use components, such as forms, menus, pagination, modal boxes, tabs etc.
Getting Started
---------------
* [Installation](installation.md)
* [Assets Setup](assets-setup.md)
* [Basic Usage](basic-usage.md)
Usage
-----
* [Yii widgets](usage-widgets.md)
* [Html helper](helper-html.md)
* [Asset Bundles](asset-bundles.md)
Additional topics
-----------------
* [Using the .sass files of Bootstrap directly](topics-sass.md)
* [Migrating from yii2-bootstrap](migrating-yii2-bootstrap.md)

View File

@ -0,0 +1,18 @@
Asset Bundles
=============
Bootstrap is a complex front-end solution, which includes CSS, JavaScript, fonts and so on.
In order to allow you the most flexible control over Bootstrap components, this extension provides several asset bundles.
They are:
- [[yii\bootstrap5\BootstrapAsset|BootstrapAsset]] - contains only the main CSS files.
- [[yii\bootstrap5\BootstrapPluginAsset|BootstrapPluginAsset]] - depends on [[yii\bootstrap5\BootstrapAsset]], contains the javascript files.
- [[yii\bootstrap5\BootstrapIconAsset|BootstrapIconAsset]] - contains Bootstrap icons.
Particular application needs may require different bundle (or bundle combination) usage.
If you only need CSS styles, [[yii\bootstrap5\BootstrapAsset]] will be enough for you. However, if
you intend to use Bootstrap JavaScript, you will need to register [[yii\bootstrap5\BootstrapPluginAsset]]
as well. If you'd like to use the Bootstrap icons, you'll need to add `twbs/bootstrap-icons` package to your
`composer.json` and register [[yii\bootstrap5\BootstrapIconAsset]] in your application.
> Tip: most of the widgets register [[yii\bootstrap5\BootstrapPluginAsset]] automatically.

View File

@ -0,0 +1,176 @@
Assets Setup
============
This extensions relies on [Bower](https://bower.io/) and/or [NPM](https://www.npmjs.org/) packages for the asset installation.
Before using this package you should decide in which way you will install those packages in your project.
## Using asset-packagist repository
You can setup [asset-packagist.org](https://asset-packagist.org) as package source for the Bootstrap assets.
In the `composer.json` of your project, add the following lines:
```json
"repositories": [
{
"type": "composer",
"url": "https://asset-packagist.org"
}
]
```
Adjust `@npm` and `@bower` in you application configuration:
```php
return [
//...
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
//...
];
```
## Using composer asset plugin
Install [composer asset plugin](https://github.com/francoispluchino/composer-asset-plugin/) globally, using following command:
```
composer global require "fxp/composer-asset-plugin:^1.4.0"
```
Add the following lines to `composer.json` of your project to adjust directories where the installed packages
will be placed, if you want to publish them using Yii:
```json
"extra": {
"asset-installer-paths": {
"npm-asset-library": "vendor/npm",
"bower-asset-library": "vendor/bower"
}
}
```
Then you can run composer install/update command to pick up Bootstrap assets.
> Note: `fxp/composer-asset-plugin` significantly slows down the `composer update` command in comparison
to asset-packagist.
## Using Bower/NPM client directly
You can install Bootstrap assets directly via Bower or NPM client.
In the `package.json` of your project, add the following lines:
```json
{
...
"dependencies": {
"bootstrap": "5.1",
...
}
...
}
```
In the `composer.json` of your project, add the following lines in order to prevent redundant Bootstrap asset installation:
```json
"replace": {
"npm-asset/bootstrap": ">=5.1"
},
```
## Using CDN
You may use Bootstrap assets from [official CDN](https://www.bootstrapcdn.com).
In the `composer.json` of your project, add the following lines in order to prevent redundant Bootstrap asset installation:
```json
"replace": {
"npm-asset/bootstrap": ">=5.1"
},
```
Configure 'assetManager' application component, overriding Bootstrap asset bundles with CDN links:
```php
return [
'components' => [
'assetManager' => [
// override bundles to use CDN :
'bundles' => [
'yii\bootstrap5\BootstrapAsset' => [
'sourcePath' => null,
'baseUrl' => 'https://cdn.jsdelivr.net/npm/bootstrap@5.1/dist/',
'css' => [
'css/bootstrap.min.css'
],
],
'yii\bootstrap5\BootstrapPluginAsset' => [
'sourcePath' => null,
'baseUrl' => 'https://cdn.jsdelivr.net/npm/bootstrap@5.1/dist/',
'js' => [
'js/bootstrap.bundle.min.js'
],
],
],
],
// ...
],
// ...
];
```
## Compiling from the .sass files
If you want to customize the Bootstrap CSS source directly, you may want to compile it from source *.sass files.
In such case installing Bootstrap assets from Composer or Bower/NPM makes no sense, since you can not modify files
inside 'vendor' directory.
You'll have to download Bootstrap assets manually and place them somewhere inside your project source code,
for example in the 'assets/source/bootstrap' folder.
In the `composer.json` of your project, add the following lines in order to prevent redundant Bootstrap asset installation:
```json
"replace": {
"npm-asset/bootstrap": ">=5.1"
},
```
Configure 'assetManager' application component, overriding Bootstrap asset bundles:
```php
return [
'components' => [
'assetManager' => [
// override bundles to use local project files :
'bundles' => [
'yii\bootstrap5\BootstrapAsset' => [
'sourcePath' => '@app/assets/source/bootstrap/dist',
'css' => [
YII_ENV_DEV ? 'css/bootstrap.css' : 'css/bootstrap.min.css',
],
],
'yii\bootstrap5\BootstrapPluginAsset' => [
'sourcePath' => '@app/assets/source/bootstrap/dist',
'js' => [
YII_ENV_DEV ? 'js/bootstrap.js' : 'js/bootstrap.min.js',
]
],
],
],
// ...
],
// ...
];
```
After you make changes to Bootstrap's source files, make sure to [compile them](https://getbootstrap.com/docs/5.1/getting-started/contribute/#using-npm-scripts), eg. using `npm run dist`.

View File

@ -0,0 +1,18 @@
Basic Usage
===========
Yii doesn't wrap the Bootstrap basics into PHP code since HTML is very simple by itself in this case. You can find details
about using the basics at [Bootstrap documentation](https://getbootstrap.com/docs/). Still Yii provides a convenient
way to include Bootstrap assets in your pages with a line(s) added to `@app/assets/AppAsset.php`(basic application):
```php
public $depends = [
<...>
yii\bootstrap5\BootstrapAsset::class,
// optional, Bootstrap icons
// yii\bootstrap5\BootstrapIconAsset::class
];
```
Using Bootstrap through Yii asset manager allows you to minimize its resources and combine with your own resources when
needed.

View File

@ -0,0 +1,30 @@
Html helper
===========
Bootstrap introduces many consistent HTML constructions and skeletons, which allow creating different visual effects.
Only the most complex of them are covered by the widgets provided with this extension. The rest should be composed manually
using direct HTML composition.
However, several special Bootstrap markup cases are covered by the [[\yii\bootstrap5\Html]] helper.
[[\yii\bootstrap5\Html]] is an enhanced version of the regular [[\yii\helpers\Html]] dedicated to the Bootstrap needs.
It provides some useful methods like:
- `staticControl()` - allows rendering of form "[static controls](https://getbootstrap.com/docs/5.1/forms/form-control/#readonly-plain-text)"
As [[\yii\bootstrap5\Html]] extends [[\yii\helpers\Html]], it can be used as a substitute, so you don't need them both
inside your view files.
For example:
```php
<?php
use yii\bootstrap5\Html;
?>
<?= Button::widget([
'label' => Html::encode('Save & apply'),
'encodeLabel' => false,
'options' => ['class' => 'btn-primary'],
]); ?>
```
> Attention: do not confuse [[\yii\bootstrap5\Html]] and [[\yii\helpers\Html]], be careful of which class
you are using inside your views.

View File

@ -0,0 +1,20 @@
Installation
============
## Getting Composer package
The preferred way to install this extension is through [composer](https://getcomposer.org/download/).
Either run
```
php composer.phar require --prefer-dist yiisoft/yii2-bootstrap5
```
or add
```
"yiisoft/yii2-bootstrap5": "~1.0.0"
```
to the require section of your `composer.json` file.

View File

@ -0,0 +1,50 @@
Migrating from yii2-bootstrap4
==============================
yii2-bootstrap5 is a major rewrite of the entire project (according Bootstrap 5 to Bootstrap 4 migration guide).
The most notable changes are summarized below:
## General
* The namespace is `yii\bootstrap5` instead of `yii\bootstrap4`
* The php compatibility **is limited to** `>=7.0`
* The close buttons of widgets like [[yii\bootstrap5\Alert|Alert]] or [[yii\bootstrap5|Modal|Modal]] now gets rendered
via CSS and does not have any content anymore. So be sure to remove `btn-close` class and set appropriate styles yourself
if you override it.
## Widgets / Classes
### BaseHtml
### ActiveField
### ActiveForm
There is a new constant [[yii\bootstrap5\ActiveForm::LAYOUT_FLOATING]]. It's a
[new form layout](https://getbootstrap.com/docs/5.1/forms/floating-labels/) introduced in Bootstrap 5.
### Breadcrumbs
### ButtonDropdown
### ButtonToolbar
### Carousel
### LinkPager
### Modal
Change `data-target` and `data-toggle` to `data-bs-target` and `data-bs-toggle`
### Nav
### NavBar
There is now the possibility to create an [offcanvas navbar](https://getbootstrap.com/docs/5.1/components/navbar/#offcanvas).
You can achieve this by setting the `$collapseOptions` to `false` in [[yii\bootstrap5\NavBar|Navbar]] widget and the
`$offcanvasOptions` to at least an empty array.
### Tabs
### ToggleButtonGroup

View File

@ -0,0 +1,17 @@
Using the .sass files of Bootstrap directly
===========================================
If you want to include the [Bootstrap css directly in your sass files](https://getbootstrap.com/getting-started/#customizing)
you may need to disable the bootstrap css files loaded by this extension.
You can do this by setting the css property of [[yii\bootstrap5\BootstrapAsset|BootstrapAsset]] to be empty.
For this, you need to configure the `assetManager` [application component](https://github.com/yiisoft/yii2/blob/master/docs/guide/structure-application-components.md) as follows:
```php
'assetManager' => [
'bundles' => [
'yii\bootstrap5\BootstrapAsset' => [
'css' => [],
]
]
]
```

View File

@ -0,0 +1,100 @@
Yii widgets
===========
Most complex bootstrap components are wrapped into Yii widgets to allow more robust syntax and integrate with
framework features. All widgets belong to `\yii\bootstrap5` namespace:
- [[yii\bootstrap5\Accordion|Accordion]]
- [[yii\bootstrap5\ActiveField|ActiveField]]
- [[yii\bootstrap5\ActiveForm|ActiveForm]]
- [[yii\bootstrap5\Alert|Alert]]
- [[yii\bootstrap5\Breadcrumbs|Breadcrumbs]]
- [[yii\bootstrap5\Button|Button]]
- [[yii\bootstrap5\ButtonDropdown|ButtonDropdown]]
- [[yii\bootstrap5\ButtonGroup|ButtonGroup]]
- [[yii\bootstrap5\ButtonToolbar|ButtonToolbar]]
- [[yii\bootstrap5\Carousel|Carousel]]
- [[yii\bootstrap5\Dropdown|Dropdown]]
- [[yii\bootstrap5\LinkPager|LinkPager]]
- [[yii\bootstrap5\Modal|Modal]]
- [[yii\bootstrap5\Nav|Nav]]
- [[yii\bootstrap5\NavBar|NavBar]]
- [[yii\bootstrap5\Offcanvas|Offcanvas]]
- [[yii\bootstrap5\Popover|Popover]]
- [[yii\bootstrap5\Progress|Progress]]
- [[yii\bootstrap5\Tabs|Tabs]]
- [[yii\bootstrap5\Toast|Toast]]
- [[yii\bootstrap5\ToggleButtonGroup|ToggleButtonGroup]]
## ActiveField: additional fields <span id="additional-fields"></span>
- [Range](https://getbootstrap.com/docs/5.1/forms/range/): `$form->rangeInput(['min' => 0, 'max' => 100, 'step' => 1])`
- [Color picker](https://getbootstrap.com/docs/5.1/forms/form-control/#color): `$form->colorInput()`
- [Switch](https://getbootstrap.com/docs/5.1/forms/checks-radios/#switches): `$form->checkbox(['switch' => true])`
## Customizing widget CSS classes <span id="customizing-css-classes"></span>
The widgets allow quick composition of the HTML for the bootstrap components that require the bootstrap CSS classes.
The default classes for a particular component will be added automatically by the widget, and the optional classes that you may want to customize are usually supported through the properties of the widget.
For example, you may use [[yii\bootstrap5\Button::options]] to customize the appearance of a button.
The class 'btn' which is required for a button will be added automatically, so you don't need to worry about it.
All you need is specify a particular button class:
```php
echo Button::widget([
'label' => 'Action',
'options' => ['class' => 'btn-primary'], // produces class "btn btn-primary"
]);
```
However, sometimes you may need to replace the default classes with the alternative ones.
For example, the widget [[yii\bootstrap5\ButtonGroup]] uses 'btn-group' class for the container div by default,
but you may need to use 'btn-group-vertical' instead to align the buttons vertically.
Using a plain 'class' option simply adds 'btn-group-vertical' to 'btn-group', which will produce an incorrect result.
In order to override the default classes of a widget, you need to specify the 'class' option as an array that contains the customized class definition under the 'widget' key:
```php
echo ButtonGroup::widget([
'options' => [
'class' => ['widget' => 'btn-group-vertical'] // replaces 'btn-group' with 'btn-group-vertical'
],
'buttons' => [
['label' => 'A'],
['label' => 'B'],
]
]);
```
## Navbar widget <span id="navbar-widget"></span>
The navbar widget has its peculiarities. You should define at which breakpoint the navbar collapses and the generic
style of navbar (color scheme).
You can change the color scheme and the collapse breakpoint with css classes. If not defined, the scheme defaults to
`navbar-light bg-light` and the breakpoint to `navbar-expand-lg`. For more information, see [Bootstrap documentation](https://getbootstrap.com/docs/5.1/components/navbar/):
```php
Navbar::begin([
'options' => [
'class' => ['navbar-dark', 'bg-dark', 'navbar-expand-md']
]
]);
[...]
Navbar::end();
```
If you'd like to flip the brand (icon) and toggle button positions in mobile navigation, you can do this like this:
```php
Navbar::begin([
'brandOptions' => [
'class' => ['order-1']
],
'togglerOptions' => [
'class' => ['order-0']
]
]);
[...]
Navbar::end();
```