2.3 Controller and View Hello World
In this lesson I'll show you how to start sending some requests and also how to create a first controller and view for the app.
1.Introduction2 lessons, 03:17
2.Build an App4 lessons, 24:02
3.Models, Views, and Controllers5 lessons, 48:58
4.Conclusion1 lesson, 02:49
2.3 Controller and View Hello World
Hey everyone, good to have you back. In our previous lesson, we had to digest quite a few new Phalcon specific concepts like the bootstrap file, autoloaders services, dependency management with DI containers, and initializing of application. But what we didn't do, is starting to send some request and test what we have built so far. So, in this lesson, we will start with sending some requests and also create our first controller and view. Our next chapter is fully dedicated to models, views and controllers. So, this one will be just a quick hands-on to understand how things work. So, without further ado, let's quickly jump into our browser. Actually, before the browser, let me give you a quick overview of how the Phalcon translates the URLs. So, by default in Phalcon, anything after the URL will be considered as a controller. Anything after that is the action, followed by the parameters to the action. Just like a usual URL structure. But you are not limited to this. You can always change it if needed by using the router service. Coming back to the browser, hit the basic route path to our project and see what happens. By the way, the reason we are testing the route path is because we haven't defined any routes or controllers or actions yet. So, you've got an error. Well, for starters, it's a good thing. It confirms our exception handling is working as expected. Secondly, this error says the request did not find IndexController in our project, which obviously is currently missing. But we got this error because, by default, Phalcon will always look for a controller and the action named index. Whenever you request the base URL with no controller and action provided, which means this URL is also similar to this. We can fix this error by creating a new controller. We can name it index, the controller class must have the suffix, Controller, with a capital C. And to define a controller in Phalcon is very easy. Starting with a PHP tag, you first need to create a normal class, just the way you do it in the PHP. And then extend it with the Phalcon's MVC controller class, like this. And don't forget to declare the class with a use statement, as we did in the booster file earlier. Great, now the index action, please notice that the name of any action in Phalcon also carries a suffix action. Lastly, echo some message for testing. Then if you try to access the same URL from your browser, again, you should see something like this now. The message you echoed in your controller action. One thing worth noticing here is that we haven't created any view yet but still we can see the output of index action in our screen. That is because of the echo message. Sending an output directly to the screen from the controller can be necessary at times but is not preferred as a good MVC practice. So, we need to pass the output to the view. And for that, you don't need to do anything other than creating a view. By default, Phalcon will always look for a view file named after the executed action, inside a directory, named after the executed controller. So, for example, in our case, it should be index.phtml file in the index directory. PHTML is the templating engine that Phalcon uses to render views by default. Phalcon also offer other templating engines like Volt, but for sake of this tutorial, I'm going to go with the PHTML. It's just the HTML file but you can also embed PHP code in it. You can copy the same line from the index action or write something new as you please, with the PHP tag, if you are still echoing the message. Or else, you can also write pure HTML for a static message like this. This time, testing the same URL in the browser, you should see the output coming from the view. You can also notice that the echo statement is still there in the method, but output of the action is being ignored, since, Phalcon found the required view now. Congratulations, you just started to fly with Phalcon. I'm really excited to see how our project has started to get some shape now. It's going to get better and better in coming lessons. In this lesson, we learned how to define the controllers and the views. But on a very basic level for sure. The only missing piece in this picture is models, which brings us to the preparation of the database first. So, in our next lesson we are going to create a database, add some seed data and lastly connect it with our project. I'm going to use MySQL, so make sure you have already installed it. So, see you in the next one. Till then, Ciao.