You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.
Yii2 - Converting from Bootstrap3 to Bootstrap4
This article has been written because while conversion is a largely pain-free process, there are some minor issues. These are not difficult to solve, but it is not immediately obvious where the problem lies.
1 - Install Bootstrap4 My preference is to simply use composer. Change composer.json in the root of your project:
- find the line that includes Bootstrap3.
Copy the line, and change the new line:
- change bootstrap to bootstrap4
- Now head over to https://github.com/yiisoft/ - the Yii2 repository on Github
- search for bootstrap, find the Bootstrap 4 repository and see what the current version is. As of January 2020, https://github.com/yiisoft/yii2-bootstrap4 showed the current version was 2.0.8
- Change the version string to that version number, and also change the ~ to ^
- After this, you should have something like this below, maybe with higher version numbers:
"yiisoft/yii2-bootstrap" : "~2.0.6", "yiisoft/yii2-bootstrap4" : "^2.0.8",
- Save the file, then run 'composer update'
- Use your IDE, text editor or whatever other means you have at your disposal to find all occurrences where bootstrap is used and change it bootstrap4. My suggestion is to search for the string
yii\bootstrap\and change it to
yii\bootstrap4\. However, be careful - your IDE may require the backslash to be escaped. For example, Eclipse will find all files with the string easily, but the search string must have double-backslashes, while the replacement string must be left as single ones.
- Now run all your tests and fix the problems that have arisen. Most of the failures will come from the fact that the Navbar no longer works, most likely. It's still there, just being rendered differently, and in my case it was invisible. This is because Bootstrap4 has changed some elements of the navbar. In my case, 14 tests failed - many of which failed due to the use of navbar content in some manner, such as looking for the Login link to infer whether the user is logged in or not.
- You're not going to fix these issues without understanding them, so take a look at https://getbootstrap.com/docs/4.0/migration/. In particular, look at what has changed regarding the Navbars. The most meaningful is that Bootstrap4 no longer specifies a background, where Bootstrap3 did.
- Open up frontend/viewslayouts/main.php in your editor, then look at your site in the browser. In my case no navbar, except for the Brand, and that is because I changed this from what was delivered as part of the Yii2 template, and it included a background. Since the rest of it was standard, there was nothing else - no ribbon strip, and no menu entries, although mousing into the area would show the links were there and could be clicked.
- Find the line that starts with
NavBar::beginand look at it's class options. In my case they were the original:
'class' => 'navbar-inverse navbar-fixed-top'
- As I said before, no background is included, so add one: bg-dark - and check again. Now there's the ribbon back again, but no menu items.
- Right-click on the ribbon and select "Inspect Element", or do whatever you have to do in your browser to inspect the page source. Looking at that starts to give you clues over what the navbar is doing. Looking at that, and referring back to the Bootstrap4 migration guide, I had the impression that neither navbar-inverse nor navbar-fixed-top were doing anything. So I removed those, and when refreshing the page, confirmed there were no changes.
- More reading on the Bootstrap website gave me the bg-dark I mentioned earlier, and for the text, navbar-light or navbar-dark produced results.
- now I had no menu items, but I did have a button that expanded the menu. Inspecting it's properties told me it was 'navbar-toggler', and the Bootstrap website told me it new to Bootstrap4, while it and was collapsed by default, 'navbar-expand' would expand it by default. That's cool - I reckon I'm going to add a setting for logged-in users that let them choose which they prefer. In the end, I opted for navbar-expand-md, which keeps it expanded unless the screen width is tight.
- Find the line that starts with
At the end of all this, I had the class line changed to something which gave me a navbar very similar to the original:
//Bootstrap3: 'class' => 'navbar-inverse navbar-fixed-top', //Changed for Bootstrap4: 'class' => 'navbar navbar-expand-md navbar-light bg-dark',
So, that fixed my navbar. Running my tests pointed me to other problems, but I won't go into those. Now that you know the process, you can find all your errors and fix them in the same way - I need to fix problems in some of my forms where the action buttons are now being rendered in a space too small to be visible. Somehow, those problems don't seem quite so insurmountable anymore.