+ project | + protected | composer.json | composer.phar | composer.lock | + app | + Controllers | + Models | + Views | + vendor | + providers | + Providers | + public | .htaccess | index.php | + css | + img | + js project/public folder, so .htaccess will be one # project/public/.htaccess <IfModule mod_rewrite.c> Options -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule> project/protected/composer.json file of approximately the same content (depending on what you need): {
"require": {
"silex / silex": "1.0. *"
},
"minimum-stability": "dev",
"autoload": {
"psr-0": {
"Providers": "",
"Controllers": "app /"
}
}
}
project/protected/Providers , and Controllers from project/protected/app/Controllers .cd / path / to / project / protected curl -s http://getcomposer.org/installer | php php composer.phar install
<?php // project/protected/Providers/ModelsServiceProvider.php namespace Providers; use Silex\Application; use Silex\ServiceProviderInterface; class ModelsServiceProvider implements ServiceProviderInterface { public function register(Application $app) { $app['models.path'] = array(); $app['models'] = $app->share(function($app) { return new Models($app); }); } public function boot(Application $app) { } } class Models { private $app; public function __construct(Application $app) { $this->app = $app; } public function load($modelName, $modelMethod, $data = array()) { require_once $this->app['models.path'] . $modelName . '.php'; $Model = new $modelName($this->app); return $Model->$modelMethod($data); } } project/protected/app/Models directory using the $app['models']->load('Class', 'Method', $data) construction with the ability to transfer the required data to it $data . It remains only to register our provider in Silex.mount method. Let's see how the index.php file, the simplest Controller and Model will look like. <?php // project/public/index.php require_once __DIR__ . '/../protected/vendor/autoload.php'; $app = new Silex\Application(); $app->register(new Providers\ModelsServiceProvider(), array( 'models.path' => __DIR__ . '/../protected/app/models/' )); $app->mount('/', new Controllers\Index()); $app->run(); <?php // project/protected/Controllers/Index.php namespace Controllers; use Silex\Application; use Silex\Route; use Silex\ControllerProviderInterface; use Silex\ControllerCollection; class Index implements ControllerProviderInterface { public function connect(Application $app) { $index = new ControllerCollection(new Route()); $index->get('/', function() use ($app) { $label = $app['models']->load('Pages', 'index'); return $label; }); $index->get('/{name}', function($name) use ($app) { $name = $app['models']->load('Pages', 'hello', $name); return "Hello{$name}"; }); return $index; } } connect() method tells Silex that the routes described inside must be processed as part of the controller that we indexed into index.php (in this case, the base URL for this controller is the application root - /). Next, the $index variable is created, it represents something like a $app and has only routing functions. The routes themselves are written as usual. <?php // project/protected/Models/Pages.php class Pages { public function index() { return "Index"; } public function hello($name) { return ", {$name}!"; } } Source: https://habr.com/ru/post/160841/
All Articles