-
-
Notifications
You must be signed in to change notification settings - Fork 2
2. Routing
The very first thing that you need to do when building your application is structuring your URIs in a flexible and scalable way that is agnostic of how your application files are structured. This framework does it all for you. What you need to focus your effort on is planning where the URIs should go.
A route file is provided for you /application/config/routing/web.php
. This is where your routes should be defined. There are 4 route methods available
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::delete($uri, $callback);
Your first reaction might be that HTML forms
do not support PUT
and DELETE
. True that and the framework is capable of doing that work for you. You can check here how it is done.
The most basic route provides a simple way to execute any method (through Closure) upon the definition of the route.
try {
Route::get('helloworld', function() {
echo 'Hello World';
});
} catch (Exception $e) {
echo $e->getMessage();
}
You should be able to access it by navigating to http://your-app/helloworld
in your browser. If you have not changed the name of the app folder caligrafy
then you could use http://localhost/caligrafy/helloworld
.
In this tutorial, we test if the Caligrafy framework has been installed properly, we explain how basic routing can be achieved and we create a couple of basic routes.
It is not unusual to want to capture variables from the URI to act on them. For example if you want to access all the information on a specific book, you would want the URI to be http://your-app/books/1
where 1
is the id of the book that you want.
In order to do that with this framework you need to use {variablename}
syntax:
Route::get('books/{id}', function() {
// any code would go here
});
By doing so, any value that you put after books/
will be captured and you will be able to use it in your code. So if you navigate to http://your-app/books/1
, your code will be able to capture 1. Similarly, if you replace the one with another value, your code will be able to capture that value. Later you will learn how you can fetch this value in your code.
In an MVC structure, you most likely want to route to a controller to hand off the logic to a controller class that will handle executing that logic. In order to do this, you could define a route that will call the controller:
Route::get('books', 'BookController');
This route will redirect all calls to http://your-app/books
to the controller BookController
.
You could also specify a specific method from the BookController
to execute:
Route::get('books', 'BookController@read');
This route would call the method read
from BookController
.
Later on, you will learn how to create controllers.
This video explains how to create more advanced routes in Caligrafy. You will learn how to create routes with parameters and how to redirect a route to a controller or to a specific action in a controller.
HTML forms do not support PUT
and DELETE
. The framework does this for you from the moment you include a hidden _method
field in the form with a value of PUT
or DELETE
. This is called method spoofing.
This can be achieved using regular HTML:
<form action="/books" method="POST">
<input type="hidden" name="_method" value="PUT">
</form>
Or, using Pug directives that will do it automatically for you:
form(action="/books", method="POST")
=methodField('PUT')