laravel - veszprémi technology meetup
DESCRIPTION
Szekeres Bálint - Laravel PHP Framework - Fejlesszünk mint a profik Minek framework? Miért Laravel? Ha a Hello World PHP-ban már a kisujjadban van, és tudod, hogy mit jelent az n:m adatbázis kapcsolat, akkor elmesélem neked, hogy legyél Laravel sensei!TRANSCRIPT
LARAVEL PHP FRAMEWORKFejlesszünk mint a profik
FEJLESSZÜNK
LinuxApacheMySQL
PHP Laravel
FRÉMWÖRKÖK‣ Átlátható adatszerkezet‣ Szép kód, gyors fejlesztés‣ MVC pattern‣ Biztonság‣ Performance eszközök
‣ Csapatmunka‣ Tesztelhetőség‣ Bejáratott technikák‣ Cache‣ Munkalehetőségek
LARAVEL
‣ Eloquent ORM‣ Blade templating engine‣ Artisan CLI‣ Migrálás, seedelés‣ Composer packages‣ RESTful API‣ Route Model Binding‣ Localization‣ HTML class
Laravel is an open source web application framework written in PHP 5 and released under the MIT License.
FLAT PHP vs. LARAVEL// functions.phpfunction sql_connenct() { ... }
function html_start(...) { ... }
function html_end() { ... }
// users.php<?phprequery_once('functions.php');html_start('Felhasználók', true, false);$users = mysql_query('SELECT * FROM users');echo '<table style="width: 100%;">';while($user = mysql_fetch_assoc($users)) { echo '<tr>'; echo '<td>'.$user['nev'].'</td>'; echo '<td>'.$user['mail'].'</td>'; echo '<td>'.$user['tel'].'</td>'; echo '</tr>';}echo '</table>';html_end();
// app/routes.phpRoute::get('users', 'UserController@showUsers');
// app/models/User.phpclass User extends Eloquent { protected $table = 'users';}
// app/controllers/UserController.phpclass UserController extends BaseController { public function showUsers() { $users = User::all(); return View::make('users.index') ->with('users', $users); }}
// app/views/users/index.blade.php<table id="users">@foreach ($users as $user) <tr> <td>{{ $user->nev }}</td> <td>{{ $user->mail }}</td> <td>{{ $user->tel }}</td> </tr>@endforeach</table>
SZERKEZETE
laravel-project/ Projekt könyvtár
app/ config/ Adatbázis, mail, cache, session, view beállítások controllers/ Controllerek: logika, kommunikáció modellekkel database/ Adatbázis migráció, seedelés lang/ Nyelvi fájlok models/ Modellek: Eloquent osztályok, adatbázis kommunikáció start/ Globális, lokális egyéni szabályok, Laravel osztálybetöltések storage/ Ideiglenes fájlok tests/ PHPUnit tesztek views/ View fájlok (Blade templating engine) filters.php Szűrők, melyeket route-okhoz lehet kötni routes.php Route-ok: lekérésekhez controller társítás, filterek használata
public/ Laravel bootstrap és publikusan elérhető fájlok: CSS, JS, képek
vendor/ Third-party függőségek (Composer Dependency Manager)
LARAVEL MŰKÖDÉSE
Routing
Controller
View Model
Adatbázis
Böngésző
Filtering
LARAVEL A GYAKORLATBANHírportál
‣ Főoldal‣ Kategória nézet‣ Hír nézet‣ Szerző nézet‣ Admin felület
‣ Hír és kategória között n:m kapcsolat‣ Hír és szerző között n:m kapcsolat
EER DIAGRAM
MIGRÁLÁS - AUTHORS
<?php
use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;
class CreateAuthorsTable extends Migration { public function up() { Schema::create('authors', function(Blueprint $table) { $table->increments('id'); $table->string('email'); $table->string('password'); $table->string('nev'); $table->timestamps(); $table->softDeletes(); }); } public function down() { Schema::drop('authors'); }}
MIGRÁLÁS - AUTHOR-POST
<?php
use Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;
class CreateAuthorPostTable extends Migration { public function up() { Schema::create('author_post', function(Blueprint $table) { $table->integer('author_id')->unsigned(); $table->foreign('author_id')->references('id')->on('authors'); $table->integer('post_id')->unsigned(); $table->foreign('post_id')->references('id')->on('posts'); }); } public function down() { Schema::drop('author_post'); }}
ELOQUENTclass Author extends Eloquent {
protected $table = 'authors'; public function posts() { return $this->belongsToMany('Post'); // n:m kapcsolat }}class Post extends Eloquent {
protected $table = 'posts'; public function authors() { return $this->belongsToMany('Author'); // n:m kapcsolat } public function categories() { return $this->belongsToMany('Category'); // n:m kapcsolat }}class Category extends Eloquent {
protected $table = 'categories'; public function authors() { return $this->belongsToMany('Post'); // n:m kapcsolat }}
ROUTING
Route::get('/', 'HomeController@showIndex');Route::get('post/{link}', 'PostController@showPost');Route::get('categories', 'CategoryController@showIndex');Route::get('categories/{id}', 'CategoryController@showCategory');Route::get('author/{id}', 'UserController@showProfile');
Route::group(array('before' => 'auth'), function() {
Route::get('settings', 'UserController@showSettings'); Route::post('settings', 'UserController@postSettings');
});// admin => app/filters.phpRoute::group(array('before' => 'admin'), function() {
Route::get('admin', 'AdminController@showIndex'); Route::get('admin/new-post', 'AdminController@newPost'); Route::post('admin/new-post','PostController@newPost');
});
app/routes.php
CONTROLLER
class PostController extends BaseController { public function showPost($link) { $post = Post::where('link', '=', $link)->with('authors', 'categories')->first(); return View::make('layouts.post')->with('post', $post); }}
class CategoryController extends BaseController { public function showIndex() { $categories = Category::all(); return View::make('layouts.categories')->with('categories', $categories); } public function showCategory($id) { $category = Category::where('id', '=', $id)->with('posts')->get(); // eager load return View::make('layouts.category')->with('category', $category); }
}
VIEW// app/views/layouts/post.blade.php
@extends('default')
@section('content') <h1>{{ $post->cim }}</h1> Szerzők: @foreach ($post->authors as $author) <div>{{ $author->nev }}</div> @endforeach <br /> {{ $post->tartalom }}@stop
LARAVELSzekeres Bálint - valentinx