Kategorie: CakePHP 3.x

CakePHP 3: Kurzfassung zum Verwenden von Migrations

CakePHP bietet mit Migrations die Möglichkeit, Änderungen an der Datenbankstruktur einzuspielen oder rückgängig zu machen. Der typische Ablauf zur Migration ist folgender:

Generating migrations from an existing database

If you are dealing with a pre-existing database and want to start using migrations, or to version control the initial schema of your application’s database, you can run the migration_snapshot command:

bin/cake bake migration_snapshot Initial

Generating a diff between two database states

You can generate a migrations file that will group all the differences between two database states using the migration_diff bake template. To do so, you can use the following command:

bin/cake bake migration_diff NameOfTheMigrations

Applying Migrations

Once you have generated or written your migration file, you need to execute the following command to apply the changes to your database:

bin/cake migrations migrate

Migrations Status

The Status command prints a list of all migrations, along with their current status. You can use this command to determine which migrations have been run:

bin/cake migrations status

CakePHP 3: Benutzername oder Email zum Anmelden mit AuthComponent verwenden

In den Einstellungen der AuthComponent lässt sich festlegen, welches Feld der Datenbank für das Login verwendet werden soll. Hier kann allerdings nur ein einzelner Wert angegeben werden (bspw. username oder email). Um jedoch bei der Eingabe des Logins sowohl Benutzername, als auch Email zu erlauben, kann man die Einstellung zur Laufzeit (also beim Aufruf des Controllers) anpassen und das entsprechende Feld setzen. Und zwar wie folgt:

src/Controller/AppController.php

use Cake\Controller\Controller;

class AppController extends Controller
{
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'authenticate' => [
                'Form' => [
                    'fields' => ['username' => 'username', 'password' => 'password'],
                ]
            ],
        ]);
    }
}

src/Controller/UsersController.php

use App\Controller\AppController;
use Cake\Validation\Validation;

class UsersController extends AppController
{
    public function login()
    {
        if ($this->request->is('post')) {

            if (Validation::email($this->request->getData('username'))) {
                $this->Auth->config('authenticate', [
                    'Form' => [
                        'fields' => ['username' => 'email']
                    ]
                ]);
                $this->Auth->constructAuthenticate();
                $this->request->data['email'] = $this->request->getData('username');
            }

            $user = $this->Auth->identify();

            if ($user) {
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }

            $this->Flash->error(__('Invalid combination of username/email and password.'));
        }
    }
}

src/Template/Users/login.ctp

<php
echo $this->Flash->render('auth');
echo $this->Form->create();

echo $this->Form->input('username');
echo $this->Form->input('password');

echo $this->Form->button(__('Login'));
echo $this->Form->end();
?>