laravel my cookbook

6
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 book’s 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 isn’t provided with the URL, then the value of the $genre variable will be equal to null, and the message Books index. will be displayed.

Upload: robert-iulian

Post on 28-Dec-2015

18 views

Category:

Documents


6 download

DESCRIPTION

Laravel cookbook

TRANSCRIPT

Page 1: Laravel My Cookbook

Basic Routing 53<?php

// app/routes.phpget,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 book’s index route by using an optional parameter. Aparameter can be made optional by adding a question mark (?) to the end of its name. For example:

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

If a genre isn’t 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 sectionRoute::get('/books/{genre?}', function($genre = 'Crime')

{return "Books in the {$genre} category.";});

Page 2: Laravel My Cookbook

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.phpRoute::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

Robert Iulian, 05/21/14,
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
Page 3: Laravel My Cookbook

// app/routes.phpExemplu simplu de redirectRoute::get('first', function() {

return Redirect::to('second');});

Route::get('second', function() {

return 'Second route.'; });

Exemplu de redirect folosind Autentificatorul// app/routes.phpRoute::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 aren’t merely limited to using View and Redirect. We could also create our own response object to suit our needs. Let’s have a look at how this can be done.

// app/routes.phpRoute::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

Page 4: Laravel My Cookbook

the result or how long a piece of content should be cached for. However, we are able to define custom headers as we please. Let’s have a look at how we can set some header values.

// app/routes.phpRoute::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 asimple 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? Let’s have a look at this method in action.

// app/routes.phpRoute::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 foryou using the Response::download() shortcut. Let’s 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

Page 5: Laravel My Cookbook

displaying a response. The Response::download() method received a path to a file which will beserved 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 (I’m a Teapot) and a header value ofiron=man.