First steps: A Controller
So go to your main site (ex. http://localhost/fuelphp/public/index.php) including the index.php in your URL. This is your main driver for the site. It will handle all the routing of your different pages. So anything that comes after the URL, will be handled by your controllers.
Add the following to your url and try to navigate there
http://localhost/fuelphp/public/index.php/about
You should get a 404 error. That is because we haven't setup a controller to handle that request yet.
Controllers are simply a tool that fetches the requested data and serves up a view. It is the thing that a user will call, and it will communicate between your models and views. You could have JUST a controller with no models or views if you want. Let's do that to start.
So navigate to /fuelphp/fuel/apps/classes/controller/
This is where all our controllers will be stored.
Create a file called "about.php" **note the lowercase, FuelPHP requires this filename to be lowercase!
put the following code in the file.
//My first Controller <?php Class Controller_About extends Controller { public function action_index() { echo "This is my first attempts at MVC architecture."; } public function action_water() { echo "i'm thirsty"; } } ?>
Now push that to your server & reload the page, you should see some output to your browser.
The framework is handling the request to ...index.php/about by serving up the Controller_About class. So think of these classes as part of your URL
We could make any class here, Controller_Hello, then access it using ...index.php/hello.
We must use the prefix Controller_
We must also extend the class Controller
The capitalization on the word About doesn't matter.
the "action_index()" is the default function that Controller_About will call when requested. That is why it's output showed up when you requested ...index.php/about.
Access our Controller_About and the index function directly by appending this onto your URL, ...index.php/about/index
now try ...index.php/about/water
you should have gotten different output
so basically we are just accessing classes and functions in our URL's.
you need to prefix these functions with the word action_ if you wish to call them from the web.
you could easily place these in subdirectories, you just need to add the subdirectory in your class name. So if you put the controller in /fuelphp/fuel/app/classes/controller/test/about.php, your class name would be Controller_Test_About. Then you would access it using ...index.php/test/about
So we made a page echo?
"big deal. i coulda done that in one line."
Yea but we did some fancy pants stuff here. We have 2 different outputs from the same file, but different URLs. Let's do a little more with controllers