Note
Although not required, it is a good practice to append the controller name with the word controller, like HelloController / TestController.
The basic Speedy Application consists of a route (path) that will take user to a particular page, but before the page can be displayed the application may need to process or apply some logic to the end page or maybe authenticate a user so the request is passed on to a controller and then to the view which is returned to the user in the end.
The file containing the application routes is App/Routes.php.
Routes are defined along with their request methods. Let us define a route called hello which can be called using the GET method:
Route::get('/hello','HelloController@sayHello');
The above code states that the request on url /hello can be accepted using the GET method.
Here HelloController is the name of the controller to be called and sayHello is the method in that controller to be invoked.
Open up your terminal or command window and navigate to your Speedy installation directory and key in the following command:
php creator create:controller HelloController
The command above creates a controller named HelloController and places it in the /Controllers directory.
Controllers can also be made manually and then put into the Controllers directory but if you use this approach you will need to put the initial code of the controller yourself or the controller will not work as expected.
Although not required, it is a good practice to append the controller name with the word controller, like HelloController / TestController.
public function sayHello()
{
return view('hello',['name'=>"Jhonny"]);
}
The function returns a view named hello and passes a variable named name to it.
The view is the page that will be shown to the user. It is this page where all the processed data is rendered.
The Views directory holds all the views for the application.
All views should be stored in the /Views Directory and all of them should end with the extension .vu.php . E.g. hello.vu.php
<html>
<head>
<title>My First Page</title>
</head>
<body>
<h2> Hello {{name}} </h2>
</body>
</html>
The view file contains the basic HTML code and a variable declared as {{name}}, this is the variable we passed from our controller and wil be replaced with it.
Your code is ready to run.
If you do not have a server software installed, go to your terminal / command window and navigate to the project directory and start the Speedy test server by entering the following command:
php creator run:server
Head over to the browser and open up the appropriate link:
If you have a server software installed you should open up your public directory path followed by path to be accessed - /hello for your test page.
If you are using the Speedy test server then head over to http://localhost:9090/hello .