49
From CakePHP to Laravel A look at PHP Frameworks

From CakePHP to Laravel

Embed Size (px)

Citation preview

From CakePHP to LaravelA look at PHP Frameworks

JMac

JMac != framework expert

Talk about the Talk• “Hello php[world]!” using the Top 5 frameworks

• Code

• Pros/Cons

• Performance

• Deciding on a Framework• Understanding the Hype Curve

• Checking the Pulse

• Tips for future-proofing

PHP is obsessed with frameworks

Hello php[world]!

CakePHP

<?phpApp::uses('Controller', 'Controller');!class HelloController extends Controller { public $uses = array('Message');! public function index() { $this->layout = false; $message = $this->Message->findById(2); $this->set('message', $message); }}

<?phpApp::uses('Model', 'Model');!class Message extends Model {}

<?phpecho htmlspecialchars($message['Message']['message'], ENT_COMPAT, 'UTF-8');

Model Controller

View

+Pros and -Cons

+ Shallow learning curve

+ Convention over Configuration

+ Several built-in components and helpers

- Not modern PHP OOP

- Arrays for all the things!

- Code Smells

- No Composer (2.x)

yii

<?phpnamespace app\controllers;!use Yii;use yii\filters\AccessControl;use yii\web\Controller;use yii\filters\VerbFilter;use app\models\LoginForm;use app\models\ContactForm;use app\models\Message;!class SiteController extends Controller { public function actionHello() { $this->layout = false;! $message = Message::find() ->where(['id' => 2]) ->one();! return $this->render('hello', [ 'message' => $message ]); }}

<?phpnamespace app\models;!use yii\db\ActiveRecord;!class Message extends ActiveRecord { public static function tableName() { return 'messages'; }}

<?phpuse yii\helpers\Html;!echo Html::encode($message->message);

Model Controller

View

+Pros and -Cons

+ Very Trim

+ Composer

+ Modern PHP OOP with namespacing

- Tight coupling between MVC

- Disorganized documentation

Phalcon

<?phpclass HelloController extends \Phalcon\Mvc\Controller { public function indexAction() { $message = Message::findFirst(2);! $this->view->setVar('message', $message); }}

<?phpclass Message extends \Phalcon\Mvc\Model { public function getSource() { return 'messages'; }}

<?phpecho htmlspecialchars($message->message, ENT_COMPAT, 'UTF-8');

Model

Controller

View

+Pros and -Cons

+ Very, very trim

+ Modern PHP OOP with namespacing

- Compile PHP extensions

- Disorganized documentation

Laravel

<?phpclass HomeController extends BaseController { public function showHello() { $message = Hello::find(2);! return View::make( ‘message', compact(‘message') ); }}

<?phpuse Illuminate\Auth\UserTrait;use Illuminate\Auth\UserInterface;use Illuminate\Auth\Reminders\RemindableTrait;use Illuminate\Auth\Reminders\RemindableInterface;!class Hello extends Eloquent { protected $table = 'messages';}

<?phpecho htmlspecialchars($message->message, ENT_COMPAT, 'UTF-8');

Model Controller

View

+Pros and -Cons

+ Composer

+ Modern PHP OOP with namespacing

+ Good Documentation

+ Elegant Design Patterns

- Custom ORM and templating engine

- Heavy use of Reflection

- Object Registry

Symfony 2

<?phpnamespace JMac\DemoBundle\Controller;!use Symfony\Bundle\FrameworkBundle\Controller\Controller;!class HelloController extends Controller { public function indexAction() { $message = $this->getDoctrine() ->getRepository('JMacDemoBundle:Message') ->find(2);! return $this->render( 'JMacDemoBundle:Hello:index.html.php', array('message' => $message) ); }}

<?phpnamespace JMac\DemoBundle\Entity;!use Doctrine\ORM\Mapping as ORM;!/** * Message * * @ORM\Table(name="messages") * @ORM\Entity */class Message{ /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id;! /** * @var string * * @ORM\Column(name="message",

<?phpecho $view->escape($message->getMessage());

Model Controller

View

+Pros and -Cons

+ Excellent Documentation

+ Composer

+ All the Components

- Overchoice (yml, xml, twig, etc)

- Heavy use of Reflection

- Steepest Learning Curve (terminology)

A point about performance

siege -br 100 -c 10 http://phalcon.local/hello/

Performance BenchmarksRe

ques

t Per

Sec

ond

0

125

250

375

500

Framework

Phalcon CakePHP Yii Laravel Symfony2

Frameworks are slow

deciding which framework to use

the hype curve

yii, phalcon

laravel cakephpsymfony

Checking the pulse

When to jump ship…• Lack of feature development

• Hurts more than it helps

• End of Life (CodeIgnitor?)

“On a long enough timeline, the survival rate for everyone

drops to zero.”

future-proofing your code

learn PHP

avoid custom components

fat models skinny controllers

service objects

service objects

why you don’t need a framework

<rant>

Performance BenchmarksRe

ques

t Per

Sec

ond

0

1,000

2,000

3,000

4,000

Framework

HTML PHP PHP + DB Phalcon CakePHP Yii Laravel Symfony2

</rant>

Let’s talk!@gonedark

[email protected]