How to make a microframework version of Laravel? - php

I'm thinking about making a stripped-down version of Laravel that can be used as a micro-framework, kinda like the Silex of Symfony world.
I guess i need to use the Illuminate\Routing component of Laravel (available via Packagist) and make a new Router instance and then call ->dispatch on the router with a Request object but i'm not quite sure if that's the right way to do it.
I'd appreciate your suggestions/solution.

This Mohammad Gufran's blog post entitled "DRIVE YOUR APPLICATION WITH ILLUMINATE ROUTER" could give you a preview of what could be done.
To sum up:
Initialize your project using Composer.
Define the dependencies, namely: "illuminate/routing": "4.1.*" and "illuminate/events": "4.1.*".
Create the "index.php" and "routes.php" files as suggested.
Et voilĂ !
Excerpt from routes.php:
$app['router']->get('/', function() {
// Because 'Hello, World!' is too mainstream
return 'Are you looking for me ?';
});

Lumen "The stunningly fast micro-framework by Laravel" came to light.
Excerpt:
<?php
/**
* Reimagine what you expect...
*/
$app->get('/', function() {
return view('lumen');
});
/**
* From your micro-framework...
*/
$app->post('framework/{id}', function($framework) {
$this->dispatch(new Energy($framework));
});

Well, in order to make "stripped" version of Laravel you will have to dig deeper. Laravel is made of couple of proven architectural and design patterns.
Inside we can find:
Service oriented architecture in the form of Service Providers
Of course client/server implementation in the form of extending
Symfony HTTP components
Clever usage of Event driven design
Communication with app through CLI commands
MVC specific implementation with Blade templating and Eloquent ORM in
the DB layer.
Router as standalone unit.
Levereging Composer package capabilities.
And finally, in Laravel,everything is connected together with custom Application layer. I am fascinated with Laravel design. There is no framework that taught me more. So, to answer you question - it is possible to create your version of Laravel, but better solution is to accept what is Laravel offering, join community and improve it with you own ideas.

Laravel released Lumen, the official micro-framework for Laravel. It uses nikic/fast-route for routing instead of Symfony's Routing component for better performance.

Actually Laravel uses Symfony components (along with others) and the HTTP Foundation is built on Symfony HTTP Foundation and router is also from Symfony.
So, instead of digging into Laravel, check the Symfony components and you may easily build a micro framework using Symfony components (utilizing the composer).
For building the Foundation you may need these components:
HttpFoundation
Routing
HttpKernel
Read the manual, it's easy and you can make it easily and also follow the framework's source that used Symfony components to get an idea how to use these components.

Have a look Micro Version of Laravel Framework here.

Related

Symfony2 Web Service

I'm currently designing a new web application based on Symfony Framework that will serve both web and mobile accesses. For illustration purposes I will use the default AcmeDemoBundle from Symfony`s framework to elaborate my question.
So, DemoBundle gives me the following route:
/hello/{name}
So far so good. Now, I want to implement an API in this project that will serve mobile apps, so the route for the same controller as the route above whould be, for instance:
/api/v1/hello/{name}
My doubt is: what's the best way of doing that without replicating code? I intend to use the FOSRestBundle for the API and I know that he handles rendering HTML views to, but I'd like to separate the API routes from the web as I put before. Maybe should I create a new bundle for the API?
Create a bundle for the API is not a good practice for the simple reason that you can't easily re-use the bundle in an other project.
For example, you create PostBundle and his API bundle, APIPostBundle for your A project.
If you want to use that bundle for a B project, you need PostBundle and APIPostBundle. You created dependency between two bundles and it's a bad practice (Component-Based Development).
So, you have to generate an independency bundle with a strength architecture who can allow you less modification if you have to.
Use a service for the logical instructions. Then, the controller/APIController can manage the route and will call the service methods. By that way, you can just modify the service without editing the controllers.
So, your architecture looks like that:
PostBundle
Controller
- PostController
- APIPostController
Service
- PostService
- PostServiceInterface

Can I just copy/paste Laravel framework folder structure?

My website already have MVC architecture. I want to expand his capabilities by installing Laravel framework.
Can I just copy unzipped Laravel framework, so all folders and libraries are just copy/pasted. ?
I have VPS Ubuntu 12.04 and PHP 5.4.4
In which form your website has MVC architecture implemented ?
Is that through your plain PHP code or through some other framework ?
Laravel is implementing MVC architecture on its own way. In order to use that Laravel MVC capabilities, you will have to move all your logic inside Laravel.
UPDATE WITH SOME RESOURCES:
Official Laravel documentation
Laracasts - video lessons
Laravel IRC channel.
Good luck
The fact that you have used MVC does not mean that it will fit laravel's structure unfortunatelly. With different file structure, routing, controllers and models it wont work that way.
I dont know how advanced and expanded your page is, but you will probably re-write it laravel way.
Steps that you will need (assuming that you have some knowlege about laravel):
Prepare models for your existing database
Add routes for pages that you will need, create controllers for them using models
If you have used smarty templates, you can install smarty bundle for laravel, otherwise you will have re-write you templaets to blade (default laravel's templating engine)

Design Pattern/Workflow for a JavaScript / Ajax / PHP - Application

I'm creating a small Web-Application with PHP, JavaScript, Ajax and of course HTML and CSS (and a bit jQuery).
Is there a Design Pattern which you recommend?
I know MVC, but only in Java, C++.
I'm just starting with PHP and JavaScript, and search for a source which can tell me how to use these languages correctly (in form of how they should interact with each other)
My Idea is to use PHP only for DB connects and store all data "locally" in JavaScript vars, and work with them.
So, my questions:
Are there any Frameworks (should be very small, i want to code most
of the functions on my own)
Is there a book/website which can train me to do it the right way? (or do you have any links?)
There are lot of web application frameworks in PHP out of which few are really famous in development world.
cakephp http://cakephp.org/
codeigniter http://codeigniter.com/
Both of them has rich support for MVC pattern and almost similar to spring and struts in java world.
A valid answer should not refer to Zend Framework, Code Igniter or Cake PHP.
In my opinion Silex could be the framework you search. It is a microframework which uses some parts of Symfony2 but is very lightweight and very easy to learn.
From the docs:
Silex is a PHP microframework for PHP 5.3. It is built on the shoulders of Symfony2 and Pimple and also inspired by sinatra.
A microframework provides the guts for building simple single-file apps. Silex aims to be:
Concise: Silex exposes an intuitive and concise API that is fun to use.
Extensible: Silex has an extension system based around the Pimple micro service-container that makes it even easier to tie in third party libraries.
Testable: Silex uses Symfony2's HttpKernel which abstracts request and response. This makes it very easy to test apps and the framework itself. It also respects the HTTP specification and encourages its proper use.
And the simplest application would just require something like this (from the docs):
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello '.$app->escape($name);
});
$app->run();
This does everything a Controller does and even more. You then could add Twig to have a solid start for the View part. And for the Model part you could add Doctrine2 which has a great Object Relational Mapper (ORM) and even a Object Document Mapper (ODM) if you like NoSQL.
I recommend you to use Zend Framework. It's not "light" framework but is very modular and scalable. So you can use only few modules that you need and write rest of code by yourself. Another advantage is that is one of the most commonly used framework and have great documentation. Please see also javascript framework which can be useful for your project - http://knockoutjs.com/

The difference between Symfony and Silex

I would like to try Silex but i've some questions.
I know to use Symfony2 and i would like to know if Silex is very different of Symfony or it's the same thing (same operation, same code... ) ?
Moreover, Silex is recommanded for small PHP projects and Symfony for medium or big projects , it's true ?
A few things worth noting:
Silex is based on the Symfony2 components, just like the Symfony2 framework is. As such, it can be considered an alternative user interface to the components (the user being a web developer).
Since they use the same basis, migration between them should be relatively easy.
Just like Symfony2, Silex is mostly a controller framework. It provides you with some structure, but the model and view parts are handled by third-party libraries (such as Twig or Doctrine).
Since your business logic should not be in your controllers anyway, if you separate that code out and keep your controllers light, the limiting factor in terms of project size will only be the amount of routes you have.
That said, Silex will not give you the bundles that Symfony2 has.
Here are some interesting thoughts on when to use Silex (especially in the comments):
https://web.archive.org/web/20160131151109/http://www.testically.org/2011/10/11/is-there-a-specific-situation-when-to-use-a-php-micro-framework-like-silex/
Silex itself is pretty bare, which means that if you want more then just routing and tests you will need to add specific features (DB, Twig ...) in form of Services.
I recommend to take a look at some readymade Plates that provide you with this:
https://github.com/lyrixx/Silex-Kitchen-Edition
or
https://github.com/ivoba/superleansilexplate (thats mine :))
Another point is that Silex has a probably lighter footprint than Symfony2, so if you need a smaller & faster site, Silex is worth a consideration.
(UPDATE) Since Symfony 2.8 You can use symfony 2 as a microframework with a micro kernel controller. See short description here: symfony.com/blog/new-in-symfony-2-8-symfony-as-a-microframework. Now Symfony gives us more control over the structure and architecture. Good alternative to Silex if You prefer the Symfony 2 style.
Comparing Silex to Symfony before 2.8 release
Silex microframework is based on Symfony but it's not exactly the same thing. Using a full-stack framework such as Symfony for a small project is simply overkilling the project.
In a microframework, you have more flexibility to choose the tools you want to use. You can make more decisions about application architecture and logic.
In a full-stack framework with some extend you would have an architecture and a logic already predefined with restrictions and limitations to its configuration.
Silex was designed to build up the tool rather than get the set tools that you might not need. I would say that for small projects in Symfony you would have to remove features - were in Silex you would have to add them.
It is also not true that Silex isn't fit for larger projects. Silex can be used with success for larger projects but remember that you would have to build up your tools to fit your requirements (if you need to customize the architecture and logic - perhaps this is the right way to go). Other than that, I would consider using Symfony instead, because Symfony already has bunch of tools available out of the box.
Bear in mind that:
To conclude, Silex is good for smaller applications and for those, it can surely replace Symfony. Silex can also be used for larger projects (but for larger applications I would recommend to use full-stack framework instead, like Symfony).
Reference for slides at: http://www.slideshare.net/dustin.whittle/silex-from-micro-to-full-stack . If you feel like it, go ahead and read some more about the Silex framework.
I also recommend watching this intro comparing Silex to Symfony: https://www.youtube.com/watch?v=RDVtnsoOysE.
Again, a few Pro for using Silex coming from people who actually use it: https://www.youtube.com/watch?v=OJcdHGJFfLU
Silex is good for small projects, but it can be used for big projects as well. What I like the most of Silex is that I have complete control over my project structure but it's my responsibility if my project is well organized or not.
Also I recommend it over symfony if like me, you moved your application logic to the client side using a JS framework. For me it feels an overkill to use symfony only to serve a few json requests.
Silex is based on the independent Symfony2 components and isn't truly considered a full-stack web application framework like Symfony is. You should only use it for very small projects that only require a few files, or you'll outgrow it pretty quickly.
Silex is a great framework for small as well as big applications. Giving structure to the project is your responsibility in Silex. As the project becomes larger, you can nicely integrate symfony components into it although the documentation for the symfony provider is not so good especially for symfony security component.
Silex is great for prototyping projects. If you know you will be using most of the symfony components then go for symfony because you will end up with integrating almost all symfony components into silex.
In my case I needed speed right out of the box and silex provided me with the speed and although I am using most of the symfony components, it is a lot faster than symfony out of the box.
At the moment I write this, Silex is in maintenance mode and it's end of life occured in June 2018.
Which means you shouldn't use it anymore for new projects.
They recommend you to use Symfony 4 instead.
Silex was really good and fast (maybe faster than symfony 4 for simple requests/requirements), but SF4 may bring some new stack and benefits to our applications.
Update: However, in all of my attempts on migrating from Silex to Symfony 4, the simple services took 4x times more to respond no matter what I do. Silex seems to do a much better job for simple micro services.
Silex is a PHP microframework. It is use for the small project.The coding style of the symfony and the silex is almost similar to the symfony.But the symfony is use for the large projects

Security component from Symfony 2.0 as standalone

I'm trying to add Symfony 2.0 ACL to my frameworkless PHP application. Because of the lack of documentation on how to use Security component as standalone I've got totally confused and I've got stucked with questions: What class to include first? Which object to instance? Is it possible to be used without models and controllers?
Any suggestion on how to start or any good link?
Thanks
The SecurityServiceProvider for Silex might be a good place to start, as it integrates all of the essential component services in a single file. Although large, you'll probably find it much easier to digest than Symfony2's SecurityBundle.
In the interest of maintaining your sanity, you should consider using a service container to organize all of these objects. In the aforementioned provider class, the Silex Application class is a Pimple instance, so you should be able to port it stand-alone Pimple with modest effort. I saw this because integrating a Pimple service container into your application should be less invasive than adopting the Silex framework.
Once you have the essential security component classes working, you should be able to following along with the ACL documentation and add additional services to your container as needed. At that point, the ACL-specific sections of the SecurityBundle might prove helpful, as you can focus in on the relevant bits. Keep in mind that there are multiple cookbook entries for ACL in the documentation.
What class to include first?
You will most likely need to include at least parts if not all of the security core, then which ever ACL implementation that you are wanting to use. You can look at the dependencies that are listed in the beginning of the ACL implementation and see what they extend. For instance, the ACL/DBAL has the following dependencies called in the header:
namespace Symfony\Component\Security\Acl\Dbal;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\Statement;
use Symfony\Component\Security\Acl\Model\AclInterface;
use Symfony\Component\Security\Acl\Domain\Acl;
use Symfony\Component\Security\Acl\Domain\Entry;
use Symfony\Component\Security\Acl\Domain\FieldEntry;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
use Symfony\Component\Security\Acl\Exception\NotAllAclsFoundException;
use Symfony\Component\Security\Acl\Model\AclCacheInterface;
use Symfony\Component\Security\Acl\Model\AclProviderInterface;
use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
use Symfony\Component\Security\Acl\Model\PermissionGrantingStrategyInterface;
But you would probably need to check each of those listed for their dependencies, and load those as well.
I would back-track through the dependencies, and keep track of what needs what. Cull those classes out into a separate location so that you have only what you need, and use some error trapping to determine that you have it all.
Which object to instance?
Your ACL. If the dependencies are all determined, and loaded, then you should be able to instantiate the ACL class object.
Is it possible to be used without models and controllers?
To be honest, I am not sure that using ACL outside of S2 is possible without a WHOLE lot of work, but if you can get it instantiated with everything it needs, then you should be able to use the object without an MVC model.
Unfortunately, from what I understand of S2, it is a full stack framework, and meant to be an all or nothing kind of thing. but if I were going to try and make it work, this would be the way I would go about it.
If u want to understand how to use use symfony2 component and how to integrate that within your project then read Fabien Potencier blog 'create your own framework'
post that will definitely help u to understand core of framework from and how to bootstrap symfony2 component in your project
there is also good document for ACL on symfony website

Categories