laravel - veszprémi technology meetup

16
LARAVEL PHP FRAMEWORK Fejlesszünk mint a profik

Upload: balint-szekeres

Post on 07-Dec-2014

7.906 views

Category:

Technology


2 download

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

Page 1: Laravel - Veszprémi Technology Meetup

LARAVEL PHP FRAMEWORKFejlesszünk mint a profik

Page 2: Laravel - Veszprémi Technology Meetup

FEJLESSZÜNK

LinuxApacheMySQL

PHP Laravel

Page 3: Laravel - Veszprémi Technology Meetup

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

Page 4: Laravel - Veszprémi Technology Meetup

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.

Page 5: Laravel - Veszprémi Technology Meetup

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>

Page 6: Laravel - Veszprémi Technology Meetup

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)

Page 7: Laravel - Veszprémi Technology Meetup

LARAVEL MŰKÖDÉSE

Routing

Controller

View Model

Adatbázis

Böngésző

Filtering

Page 8: Laravel - Veszprémi Technology Meetup

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

Page 9: Laravel - Veszprémi Technology Meetup

EER DIAGRAM

Page 10: Laravel - Veszprémi Technology Meetup

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'); }}

Page 11: Laravel - Veszprémi Technology Meetup

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'); }}

Page 12: Laravel - Veszprémi Technology Meetup

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 }}

Page 13: Laravel - Veszprémi Technology Meetup

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

Page 14: Laravel - Veszprémi Technology Meetup

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); }

}

Page 15: Laravel - Veszprémi Technology Meetup

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

Page 16: Laravel - Veszprémi Technology Meetup

LARAVELSzekeres Bálint - valentinx