PHP code example of ali1 / cakephp-param-converter

1. Go to this page and download the library: Download ali1/cakephp-param-converter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ali1 / cakephp-param-converter example snippets


 // src/ParamConverter/ConfigurableEntityConverter

namespace App\ParamConverter;

use Cake\Core\App;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use ParamConverter\Converter\EntityConverter;

/**
 * Class ConfigurableEntityParamConverter
 *
 * Alternative Param Converter for Entity classes that allows custom get methods
 */
class ConfigurableEntityParamConverter extends EntityConverter
{
    /**
     * @inheritDoc
     */
    public function convertTo(string $value, string $class)
    {
        preg_match('/^(.*)\\\Model\\\Entity\\\(.*)$/', $class, $matches);

        $tableClass = $matches[1] . '\Model\Table\\' . Inflector::pluralize(App::shortName($class, 'Model/Entity')) . 'Table';

        $table = App::shortName($class, 'Model/Entity');

        TableRegistry::getTableLocator()->set(Inflector::tableize($table), new $tableClass());
        $table = TableRegistry::getTableLocator()->get(
            Inflector::tableize($table)
        );

        $tableGetMethod = empty($table->paramConverterGetMethod) ? 'get' : $table->paramConverterGetMethod;

        return $table->$tableGetMethod($value);
    }
}

 // config/param_converter.php

return [
    'ParamConverter' => [
        'converters' => [
            \App\ParamConverter\ConfigurableEntityConverter::class,
            // \ParamConverter\Converter\EntityConverter::class,
            \ParamConverter\Converter\FrozenDateTimeConverter::class,
            \ParamConverter\Converter\BooleanConverter::class,
            \ParamConverter\Converter\IntegerConverter::class,
            \ParamConverter\Converter\FloatConverter::class,
        ],
    ],
];

 php

// src/Controller/AppointmentsController.php

class AppointmentsController extends AppController {
    public function view(Appointment $appointment): void
        {
            // users will still navigate to yoursite.com/appointments/view/65
            // but the param converter removes the need for this line: $appointment = $this->Appointment->get($id);
            // use the ConfigurableEntityConverter (see below) to use a customised getter instead of Table->get
            $this->set('appointment', $appointment);
        }
    public function onDate(FrozenDate $date): void
    {
        // navigate to yoursite.com/appointments/onDate/2023-02-22
        // $date will be the FrozenDate object
        $appointments = $this->Appointments->find('all')
            ->where([
                'Appointments.start >=' => $date->toDateString(),
                'Appointments.start <' => $date->addDay(), 'cancelled IS NULL',
            ]);
        $this->set('appointments', $appointments);
    }
}
 php

// config/param_converter.php
return [
    'ParamConverter' => [
        'converters' => [
            \ParamConverter\Converter\EntityConverter::class,
            \ParamConverter\Converter\FrozenDateTimeConverter::class,
            \ParamConverter\Converter\BooleanConverter::class,
            \ParamConverter\Converter\IntegerConverter::class,
            \ParamConverter\Converter\FloatConverter::class,
        ]
    ]
];