<?php print_r(get_loaded_extensions());
Array ( [0] => Core [1] => libxml [2] => filter [3] => SPL [4] => standard [5] => phalcon [6] => pdo_mysql )
tutorial / app / controllers / models / views / public / css / img / js /
mod_rewrite
for Apache and, based on our directory structure, we need two .htaccess files: one at the root of the project, the other in a public directory. #/.htaccess <IfModule mod_rewrite.c> RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L] </IfModule>
public/
directory. This step ensures that internal project folders remain hidden from public access, reducing the security risk. #/public/.htaccess <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L] </IfModule>
public/index.php
Bootstrap file looks like this: <?php try { //Register an autoloader $loader = new \Phalcon\Loader(); $loader->registerDirs(array( '../app/controllers/', '../app/models/' ))->register(); //Create a DI $di = new Phalcon\DI\FactoryDefault(); //Setting up the view component $di->set('view', function(){ $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); return $view; }); //Handle the request $application = new \Phalcon\Mvc\Application(); $application->setDI($di); echo $application->handle()->getContent(); } catch(\Phalcon\Exception $e) { echo "PhalconException: ", $e->getMessage(); }
Phalcon\Loader
component is used. <?php $loader = new \Phalcon\Loader(); $loader->registerDirs( array( '../app/controllers/', '../app/models/' ) )->register();
Phalcon\DI
, which acts as a link, combining the various components to work together. <?php //Create a DI $di = new Phalcon\DI\FactoryDefault();
Phalcon\DI
implementing dependency injection. It registers most of the standard framework components. Thus, we should not register them one by one. If necessary, you can easily replace it with your own implementation. <?php //Setting up the view component $di->set('view', function(){ $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); return $view; });
<?php $application = new \Phalcon\Mvc\Application(); $application->setDI($di); echo $application->handle()->getContent();
<?php class IndexController extends \Phalcon\Mvc\Controller { public function indexAction() { echo "<h1>Hello!</h1>"; } }
app/views/index/index.phtml
: <?php echo "<h1>Hello!</h1>";
app/controllers/IndexController.php
) now contains an empty action: <?php class IndexController extends \Phalcon\Mvc\Controller { public function indexAction() { } }
app/views/index/index.phtml
by adding a link to another “signup” controller: <?php echo "<h1>Hello!</h1>"; echo Phalcon\Tag::linkTo("signup", "Sign Up Here!");
<h1>Hello!</h1> <a href="/test/signup">Sign Up Here!</a>
app/controllers/SignupController.php
( app/controllers/SignupController.php
): <?php class SignupController extends \Phalcon\Mvc\Controller { public function indexAction() { } }
app/views/signup/index.phtml
). <?php use Phalcon\Tag; ?> <h2>Sign using this form</h2> <?php echo Tag::form("signup/register"); ?> <p> <label for="name">Name</label> <?php echo Tag::textField("name") ?> </p> <p> <label for="name">E-Mail</label> <?php echo Tag::textField("email") ?> </p> <p> <?php echo Tag::submitButton("Register") ?> </p> </form>
Phalcon\Tag
provides useful methods for creating form elements.Phalcon\Tag::form
method, we passed the path to the controller / action of the application that will process the form.PhalconException: Action “register” was not found on controller “signup”
<?php class SignupController extends \Phalcon\Mvc\Controller { public function indexAction() { } public function registerAction() { } }
CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(70) NOT NULL, `email` varchar(70) NOT NULL, PRIMARY KEY (`id`) );
app/models
directory.users
” table: <?php class Users extends \Phalcon\Mvc\Model { }
<?php try { //Register an autoloader $loader = new \Phalcon\Loader(); $loader->registerDirs(array( '../app/controllers/', '../app/models/' ))->register(); //Create a DI $di = new Phalcon\DI\FactoryDefault(); //Set the database service $di->set('db', function(){ return new \Phalcon\Db\Adapter\Pdo\Mysql(array( "host" => "localhost", "username" => "root", "password" => "secret", "dbname" => "test_db" )); }); //Setting up the view component $di->set('view', function(){ $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views/'); return $view; }); //Handle the request $application = new \Phalcon\Mvc\Application(); $application->setDI($di); echo $application->handle()->getContent(); } catch(\Phalcon\Exception $e) { echo "PhalconException: ", $e->getMessage(); }
<?php class SignupController extends \Phalcon\Mvc\Controller { public function indexAction() { } public function registerAction() { //Request variables from html form $name = $this->request->getPost("name", "string"); $email = $this->request->getPost("email", "email"); $user = new Users(); $user->name = $name; $user->email = $email; //Store and check for errors if ($user->save()) { echo "Thanks for register!"; } else { echo "Sorry, the following problems were generated: "; foreach ($user->getMessages() as $message) { echo $message->getMessage(), "<br/>"; } } } }
string
” filter to the entered name and the “ email
” filter to the email address to make sure that the user has not sent us any malicious characters.Users
model. Public properties of the model correspond to the fields in the users
table. By setting the values of the new model and calling the save()
method, we write to the database. The save()
method returns a Boolean value that informs us whether the record was successful.not null
(i.e., are required). If you leave the fields blank when sending the form, then in the browser you will see the message:Sorry, the following problems were generated: name is required
email is required
Source: https://habr.com/ru/post/160311/
All Articles