Вы находитесь на странице: 1из 5

Basic Routing 53

<?php

// app/routes.php
get,post,put,delete = verbs, http requests

Route::get();
Route::post();
Route::put();
Route::delete();
Route::any(); -> verifica si atribuie orice verb/metoda trimisa prin HTTP, dar nu este
recomandata

Parametrii de Route/ Route parameters

Route::get('/books/{genre}', function($genre)
{
return "Books in the {$genre} category.";
});

We could also remove the requirement for the books index route by using an optional
parameter. A
parameter can be made optional by adding a question mark (?) to the end of its name. For
example:

// app/routes.php
// routes for the books section
Route::get('/books/{genre?}', function($genre = null)
{
if ($genre == null) return 'Books index.';
return "Books in the {$genre} category.";
});

If a genre isnt provided with the URL, then the value of the $genre variable will be equal to
null, and the message Books index. will be displayed.

Daca nu vrem ca variabila $genre sa fie null ii putem atribui o valoare default:

// app/routes.php
// routes for the books section
Route::get('/books/{genre?}', function($genre = 'Crime')
{
return "Books in the {$genre} category.";
});


VIEWS

Functia de afisare a unui View. 'simple' este denumirea fisierului cu extensia blade.php. Laravel
stie de unde sa ia fisierul si trebuie doar sa introducem ce este inainte de .blade.php

// app/routes.php
Route::get('/', function()
{
return View::make('simple');
});

make() este o metoda


View Data

// app/routes.php

Route::get('/{squirrel}', function($squirrel)
{
$data['squirrel'] = $squirrel;
return View::make('simple', $data);
});

Luam parametrul $squirel din URI si-l trimite in variabila $data[squirel] pe care ulterior o trimite
in metoda make().
Metoda make() accepta ca al doilea parametru un array de date ce vor fi folosite in
view(simple.blade.php).

Important!
Laravel ia cheia fiecarui element din array si-l transforma intr-o variabila in View:
$data = array('name' => 'Taylor Otwell', 'status' => 'Code Guru')
In template(simple.blade.php) putem afisa valorile din array foarte simplu:
<?php echo $name; // gives 'Taylor Otwell' ?>
<?php echo $status; // gives 'Code Guru' ?>

Se pot stoca array-uri multidimensionale!



Redirectionari/Redirect

// app/routes.php
Exemplu simplu de redirect
Route::get('first', function() {
return Redirect::to('second');
});

Route::get('second', function()
{
return 'Second route.';
});

Exemplu de redirect folosind Autentificatorul
// app/routes.php
Route::get('books', function()
{
if (Auth::guest()) return Redirect::to('login');
// Show books to only logged in users.
});



Custom Responses

Both View and Redirect inherit from the Laravel Response object. The response object
is an instance of a class that can be handed back to Laravel as the result of a routed closure or
a controller action to enable the framework to serve the right kind of response to the browser.
Response objects generally contain a body, a status code, HTTP headers, and other useful
information.
For example, the body segment of the View would be its HTML content. The status code
for a Redirect would be a 301.
Laravel uses this information to construct a sensible result that can be returned to the
browser.
We arent merely limited to using View and Redirect. We could also create our own
response object to suit our needs. Lets have a look at how this can be done.


// app/routes.php
Route::get('custom/response', function()
{
return Response::make('Hello world!', 200);
});
HTTP headers are a collection of key-value pairs of data which represent useful information to
the web browser that is receiving the response. Normally they are used to indicate the format of
the result or how long a piece of content should be cached for. However, we are able to define
custom headers as we please. Lets have a look at how we can set some header values.

// app/routes.php
Route::get('custom/response', function()
{
$response = Response::make('Hello world!', 200);
$response->headers->set('our key', 'our value');
return $response;
});


JSON Responses

Often within our application we will have some data that we wish to serve as JSON. It could be
a
simple object or an array of values. Laravel provides a Response::json() method that will
configure the response object with a number of details that are specific to JSON results. For
example an appropriate HTTP Content-Type header.
We could set these up manually, but why bother when Laravel will do it for us? Lets have a look
at this method in action.

// app/routes.php
Route::get('markdown/response', function()
{
data = array('iron', $'man', 'rocks');
return Response::json($data);
});


Download Responses/Descarcari

Serving files directly requires certain headers to be set. Fortunately, Laravel takes care of this
for
you using the Response::download() shortcut. Lets see this in action.

// app/routes.php

Route::get('file/download', function()
{
$file = 'path_to_my_file.pdf';
return Response::download($file);
});
Now if we navigate to the /file/download URI the browser will intiate a download instead of
displaying a response. The Response::download() method received a path to a file which will be
served when the response is returned.

Route::get('file/download', function()
{
$file = 'path_to_my_file.pdf';
8 return Response::download($file, 418, array('iron', 'man'));
});
Here we will serve our file with the HTTP status code of 418 (Im a Teapot) and a header value
of
iron=man.

Вам также может понравиться