Don't be nervous, welcome to your first time with Phalcon. First of all, we'll show you how this playground works.
|
<?php
echo "<h1>Hello there!</h1>";
echo "<p>This is an interactive tutorial to learn
Phalcon PHP Framework in your browser.</p>";
for ($i=0; $i<=10; $i++) {
echo "Welcome", "<br>";
}
|
Hello there!This is an interactive tutorial to learn Phalcon PHP Framework in your browser. WelcomeWelcome Welcome Welcome Welcome Welcome Welcome Welcome Welcome Welcome Welcome WelcomeTry, learn and practice Phalcon in your browser.
or
1. IntroductionThis is a PHP playground, you can execute any kind of code and run it without have to install anything on your computer. Type the following code in the editor and then click the button "Run".2. Using PhalconPhalcon is a C extension for PHP, once you have enabled it in your server, it's not necessary include files or register autoloaders to use it, very similar to the functions bundled as part of PHP. Let's call a method in a Phalcon class:3. Extending classesAs normal PHP classes, you can extend Phalcon classes, making them base of your own classes. The class "MyText" extends from Phalcon\Text inheriting all its functionality. The method "reverseCamel" executes the previously seen "camelize" method but also returns the string reversed. The following code executes that method:4. ComponentsFunctionality in Phalcon is organized in components. For instance, Phalcon\Http\Request allows us to get information about the request. Let's see your user agent with the following code:5. Dependency InjectionSometimes, components depend on other components. In Phalcon, each component to be used within an application is registered in a global bag where any component can easily request them. Phalcon\DI it's designed to achieve this task. Now, see how we've moved the request creation to the services container.
Services can be globally accessed in the wide application. Additionally, you have control over the object initialization making your development more flexible. By the way, what is your IP address? 6. Using databasesWork with a database will make this playground more interesting. See in the editor how to connect to a database. What tables are in the database? Append this code to the existing in the editor to know that. 7. Working with ModelsA full object oriented application may use an ORM to manipulate tables and rows as objects. The class "Robots" represents the table "robots" in the database. Here, it's where we use all seen together. Append the following code to query all the robots created and print their names: 8. Organizing ClassesA new file was created to organize better our example. You can edit the code in each file by clicking its related tab at the top of the editor. Phalcon don't impose you to organize your application in certain way. We can still use the model Robots in index.php. To check this, let's count how many robots are of each type. 9. Using AutoloadersWe've used a "require" to include the content of "models/Robots.php" into "index.php". That would be fine if we have just a few models. Now, let's pretend that we have 10 models, making a "require" for each of them can be tedious. Phalcon provides a fast autoloader, it loads classes only when they're needed. Let's change the "require" by adding this code: 10. ApplicationsWith Phalcon you can create Micro, Single and Multi module applications. For the purposes of this tutorial, we'll create a micro application. In you can add routes to a handler, depending on the URL accessed by the user if there's a predefined route that matches it then the related handler will be executed. Append this code to the application and change the url in the browser to: http://remotehost/say/hello/to/Alice 11. Putting it togetherFinally, we will use all seen together, add the following path for searching robots by their type. Then visit this URL http://remotehost/robots/borg Thanks!Thanks for trying Phalcon, make yourself at home. Feel free to check the documentation and test other examples on this playground. Try the other tutorials and enjoy this framework. Don't forget share this page with your friends and colleagues! |
|
Please wait while our updates load |
|
|