Nearly completely free PHP hosting forever with InfinityFree

Looking for some free hosting for my very simple test project, I have stepped into InfinityFree offer.

They’re probably the best in the entire universe, because they bring you a fabulous offer (nearly free forever, absolutely no ads or obligations, no credit card or this kind if stuff, no caps). But… please, read the below carefully, before actually opening a champagne bottle…

Read More “Nearly completely free PHP hosting forever with InfinityFree”

Customize delete confirmation in GridView

As nearly everything in Yii 2, you are starting from scratch with out-of-the-box solution for displaying models in grids (using yii\grid\GridView). Usually last among columns ($columns) of such grid there is an button column (yii\grid\ActionColumn) that serves basic operations for corresponding row or item. One of such column’s buttons there’s a delete button.

Everything is ready for you, to be used without much effort, but only as long as you are building an English-only application, as translating or customizing delete confirmation in such scenario isn’t a super-easy task. One of a great approaches is given in here: How to create a custom delete button?.

But, it works just great, if you want to customize just a single delete button of a one or maybe two grid view. What about, if you have a grid view for each and every of your data model?

Read More “Customize delete confirmation in GridView”

One of two model’s attributes must be provided

Suppose that we have any Yii 2 model with to and from attributes. And we want to make sure that either of them is provided. So we need a logical OR relation between these two attributes.

A slightly modified Stack Overflow example will do the trick:

['from', 'required', 'when' => function($model) {
    return !is_numeric($model->to);
}, 'message' => Yii::t('models', 'Either "to" or "from" must be provided.')],

Using a closure (an anonymous function) is all the “magic” here. We use it as a value $when of property of yii\validators\Validator (actually yii\validators\RequiredValidator in this case).

To basically tell Yii 2 that it must validate whether from does not have null or empty value if, and only if to does not have null or empty value as well.

Adding new application to Yii 2 Advanced Project Template

After writing the previous article I have realized that I solved the problem quite wrong. I added the example RESTful app controller to the frontend application in the multi-application Yii 2 Advanced Project Template environment.

I have also realized that the corresponding Yii 2 Guide is very, very poor and limited. It only tells you that you can have many applications (like backend, fronted and console) in the multi-application Yii 2 Advanced Project Template environment. But it leaves you completely guessing on how to actually add new application.

This quick article is my memo to remember steps that must be undertaken in order to achieve this.

Read More “Adding new application to Yii 2 Advanced Project Template”

The quickest way to add RestAPI to your Yii 2 app. Part 1

The example given in Yii 2 Guide is of a low quality, because it It requires User model based on database, not a file. And the guide mixes two things together:

  • Yii 2 Basic Template, which is used in this example, has User model, but based on itself (on a file — list of users is given as an array)
  • Yii 2 Advanced Template has User model based on database, but it uses a multi-application concept in the same time and adding REST support, like shown in this example, isn’t possible.

As an effect, people who just starts their journey with Yii 2 and REST are often confused and finds official example not working in their side.

This article deals with this problem and show how to enable REST in Yii 2 Basic Project Template by using some model (database-based) other than User model.

Read More “The quickest way to add RestAPI to your Yii 2 app. Part 1”

Configure virtual hosts for separated apps in Advanced Project Template

When developing web applications in PHP based on Yii 2’s Advanced Project Template you have a clear separation between two applications:

  • Backend — for service’s admins
  • Frontend — for regular users

There’s also a separate console application but it isn’t important from web applications perspective as it is not accessed remotely via URL, but rather locally in console.

In local development environment you can separate access to these two applications using various methods:

  • Two separate domains — i.e. frontend.localhost and backend.localhost
  • Two subdomains — i.e. app.localhost and admin.app.localhost
  • Separate port — i.e. app.localhost and app.localhost:8080

This article deals with all three scenarios, but using XAMPP/Apache for Windows only. You must adjust provided examples, if you’re running Apache on different operating system.

Read More “Configure virtual hosts for separated apps in Advanced Project Template”

Create a non-standard primary key in Yii 2 migration

If you need a typical primary key then use $this->primaryKey() in your migration file. You can also use something like $this->primaryKey(2), if you need a bigger (here int(2)) primary key.

But, what when you need a non-standard primary key, i.e. you want to turn following example SQL code:

CREATE TABLE `country` (
  `code` CHAR(2) NOT NULL PRIMARY KEY,
  `name` CHAR(52) NOT NULL,
  `population` INT(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

into Yii 2 migration?

Calling:

'code' => $this->primaryKey()->char(2)->notNull()

will fail with error:

Exception: Calling unknown method: yii\db\mysql\ColumnSchemaBuilder::char()

Now, what Qiang?

Read More “Create a non-standard primary key in Yii 2 migration”

Introduction to curl

This is just a barely memo about very, very basics of curl. Since curl fetches URLs and data from some endpoints, it can be used for a variety of reasons. I am using to for a very basic (and quick!) API calls tests.

If you’re on Linux, Unix or any of its clones or if you’re on Mac then you most likely have curl. If you’re on Windows, but you’re using Git for Windows (or some similar tool) then you have curl.

So, let’s jump into that basics. Be warned — you will not find here anything that you wouldn’t already knew.

Read More “Introduction to curl”

Pluralized and non-pluralized rules for the same REST controller

The quickest way of adding REST support to your Yii 2 application is to add REST controller to it and configure REST rules. Just a couple of line and framework will do all the dirty stuff for you “behind the scene”.

If you do this then your REST rules will have pluralize property set to true be default meaning that even though you are using controller’s name in singular (i.e. UserController, PostController etc.) your REST URL rules will have it in plural, i.e. GET /posts/12, DELETE /posts/13 etc.

Since I am a purification and perfection maniac, I wasn’t pretty much satisfied with the idea that:

  • I have plural routes when fetching, deleting or updating single record (when pluralize = true) or
  • I have single GET /post router when fetching all records (when pluralize = false)

In other words, I wanted a solution that will give me:

  • Everything in singular (i.e. GET /post/12, PATCH /post/77)
  • Except listing all the records, where I will have plural (i.e. GET /posts)

The solution turned out to be quite simple…

Read More “Pluralized and non-pluralized rules for the same REST controller”

Different PHP versions per single hosting

Using simple one-liner in .htaccess file you can enforce different PHP version in two or more of your folders. If you have different websites / domains / services registered for each folder, you can as an effect have single hosting running two or more applications that requires different version of PHP parser.

Note that this will most likely work on shared hostings only, where your ISP provides different versions of PHP at your service and some web tool (i.e. cPanel) to switch between them. In most of such cases web tool allows you to pick the same PHP version for the entire hosting. With this solution you can have them different.

However, on your private servers and hostings you have to provide that different versions of PHP parser will be installed and made available.

Read More “Different PHP versions per single hosting”

Replacement for mb_strimwidth() which is part of php-mbstring package

One of blogs in my WordPress network is using some old, free theme that was using mb_strimwidth() function. This function is part of php-mbstring package which turned out to be disabled by default in my hosting configuration for PHP 7.2 (it is enabled by default for PHP 5.6).

After enabling PHP 7.2 in my hosting configuration it turned out one of my blogs stopped showing content (showing those damn two small smilies instead). After enabling wp_debug mode it turned out that theme was using mb_strimwidth() that was part of disabled php-mbstring package.

The mb_strlen is not part of that package, seems to be part of core PHP and can be used as a workaround.

Read More “Replacement for mb_strimwidth() which is part of php-mbstring package”

Copy remote files with copy() function

Seven years old comment in PHP manual, which still shines like a real gem! You can use copy() function to easily (one-liner!) copy remote files, without doing messy fopen() stuff:

if(!@copy('http://someserver.com/somefile.zip','./somefile.zip'))
{
    $errors = error_get_last();
    
    echo "COPY ERROR: ".$errors['type'];
    echo "<br />\n".$errors['message'];
}
else echo "File copied from remote!";

steve a h, whoever you’re or were, I salute you!

Chained, AJAX-updated listboxes in Yii2

There’s a great answer at Stack Overflow that shows, how in Yii2 you can implement a form containing listbox and two input fields. Listbox contains data from one model and when user selects any option, remaining two input fields should be populated with relevant values from related model.

I wrote an extended version of this code. It populates two other listboxes instead of just input fields. And it uses a little bit less deprecated code! :>

Read More “Chained, AJAX-updated listboxes in Yii2”

From Drupal-based blog to Yii-based application

If you are in need to migrate Drupal’s content data (articles, pages, events, etc.) to your own CMS or database structure then this article may be helpful for you.

Note however, that I have little to none knowledge of Drupal and that my MySQL experience is very limited. Therefore, information provided in this article may be useful, but on the other hand, I can write something wrong (even up to a complete bullshit) or mess things around.

Use this text with a caution and at your own risk. You have been warned. No money back guaranteed! :>

Read More “From Drupal-based blog to Yii-based application”

Installing Composer to PHP on Windows

There are a few small issues and changes, when installing and using Composer dependency manager to PHP running on localhost (local development environment) on Windows.

This article should clarify these things and answer some questions about using Composer on Windows.

Enabling HTTPS

First of all, you need to have support for HTTPS protocol and wrapper enabled in your PHP. Edit php.ini and look for extension=php_openssl.dll line. If if has ; character in the beginning (library disabled), remove it (enable library). Then restart your Apache.

Now, to install Composer.

Just a single installation

If you need it for just a single project (it’s author forces you to use it, but you normally don’t care for all these composer.json files), then you may use typical way, described at Download page. I.e. execute:

curl -sS https://getcomposer.org/installer | php

from command-line. If this does not work (or if you don’t have curl), try alternate version of:

php -r "readfile('https://getcomposer.org/installer');" | php

It this, again, don’t work, try the next method of central installation.

Central installation

In most case you’d like to use Composer for many projects. Therefore you need to install central version, that will be accessible in every path. The simplest (and the only tested by me! :>) method is to use installer file. You’ll get it at Download page or in here.

Run downloaded Composer-Setup.exe file and follow on-screen instructions.

Default settings seems fine, so clicking Next > few time should do the trick.

Once Composer is installed on your system, you should either exit all terminal windows and/or all Windows Explorer windows and open them again, to force system to re-read PATH environment variable, just updated by Composer installer. Alternatively, if above seems to be not working, restart entire Windows.

Usage

Most pages for developers and most code repositories are handled by geeks using Linux. So, they write to run php composer.phar install. This won’t work under Windows and should be replaced by simple composer install. Since Composer is (should be) listed on your PATH, it should be available in any directory.

That, of course, matches only installing “central” installation, using Composer-Setup.exe file. If you installed only single, per-project version of Composer, you’re normally running php composer.phar install.

Remove all kind of quotation marks

I say: “Remove all quotation marks from a string“. You say: “stripslashes“.

Works fine. In most of situations.

If you have to deal with quotation marks manually entered to rich editor (i.e. by copying large block of text from Word for example) or with a string being a poor effect of even more poor encoding routine, you may be sure, that stripslashes won’t do all the dirty work for you.

This is the place where my handy method comes in.

Read More “Remove all kind of quotation marks”

Dump current database state to Yii migration

There’s a wonderful extension to Yii 1.x, called database-command, written by the one and only schmunk, which allows you to quickly and easily generate any set of CDbMigrationCommands (actually, entire single migration code) based on current database schema.

This isn’t, of course, the only one out there. There are some others. But I like this one the most, mainly for flexibility (may parameters to suit generated migration file to your needs). However, if this is your first approach to using custom yiic commands, you may get a little bit confused. This article should help you.

Read More “Dump current database state to Yii migration”

Plural formula in PHP to evaluate ending for a number in Polish language

Providing correct plural form of countable words in English is easy, because there is a difference only between one (1 item) and other (0 items, 2 items, 5 items etc.).

In Polish (and some other languages) this is much harder, because Polish has two different plural forms and plural form of countable words is way, way more complicated and based sometimes not only on whole number but also on last digit of this number etc.

Read More “Plural formula in PHP to evaluate ending for a number in Polish language”