Ярлыки

.htaccess (4) тестирование (8) шаблоны проектирования (3) css (5) Debian (6) docker (2) Doctrine2 (6) Git (6) html (4) java (6) javascript (13) jquery (11) LFS (3) linux (23) mac os (4) mod_rewrite (2) MSSQL (4) MySQL (18) ORM Doctrine (17) patterns (3) PDO (3) perl (7) PHP (64) PHPUnit (8) Python (15) SEO (2) Silex (1) SimpleXML (1) SQL (14) ssh (4) Ubuntu (24) Yii1 (1) Zend Framework (19) ZendFramework2 (8)

пятница, 31 октября 2014 г.

PHP. Использование итераторов директорий.

$directory = new \RecursiveDirectoryIterator('./datafiles');
$iterator = new \RecursiveIteratorIterator($directory);
$regex = new \RegexIterator($iterator, '/\w+\.ixt/i',
                \RecursiveRegexIterator::GET_MATCH);
$files = array();
foreach($regex as $file) {
    $files[] = $file[0];
}
sort($files);
print_r($files);
Array
(
    [0] => akjkdsjff3.ixt
    [1] => dfsd8878.ixt
    [2] => skka878.ixt
    [3] => zwsdf8sdk.ixt
)

вторник, 7 октября 2014 г.

Silex. Динамическая маршрутизация (роутинг).

Как вариант:
use Silex\Application;
use App\Controller;

$app = new Application();
$app->get('/{controller}/{action}',
    function($controller, $action) use ($app)
{  
    $controllerName = ucfirst($controller) . 'Controller';
    if(class_exists($controllerName)) {
        $controller = new $controllerName;
        if ($controller instanceof Controller) {  
            if(is_callable(array($controller, $action))) {  
                return $controller->$action($app);  
            }  
        }
    }
    $app->abort(  
        404,  
        sprintf(  
            'No route found for: %s:%s',  
            $controller,  
            $action  
        )  
    );  
})  
->assert('controller', '[A-z]+')  
->assert('action', '[A-z]+')
->value('controller', 'index')
->value('action', 'index');  
$app->run();
Автозагрузку классов делаем через composer
...
"autoload": {
    "psr-4": {"": ["model/", "controller/"] }
}
...
Контроллеры наследуем от супер-класса