Structure of legacy project with Silex - php

I have a very old flat PHP project. I would like to modernize the source files without changing the big and old database structure. I found the PHP Framework Symfony Silex. I like the micro structure of this framework it's not so complicated to understand.
I have the following file structure:
- classes (The business logic)
- web (index.php and all my controllers)
- vendors
How can I include my business logic from the Silex app? Should I use require_once('classes/file1.php'); in my index.php?
How can I access the database from the business logic files?

Transitioning legacy code to a new framework is a difficult task. It's not going to be as simple as requiring your business logic files. Here's how I would go about it if I were in your position.
Spend some time learning Silex. Start by building a brand new test project with it to see how it works. It's best if you know how the framework is supposed to work before you try to integrate it with a legacy system. After you've tried it for a while, you can decide if Silex is a good fit for you.
Once you know how Silex works, you should get the Composer autoloader working with your code. Composer can load you business logic classes without having to use require.
Next, figure out how to work with your database. I see two options for you. You can either transition to Doctrine, or use your existing database access classes. The Symfony (and therefore Silex) ecosystem is oriented around the Doctrine ORM. Depending on your schema, you may be able to write Doctrine mappings for it. However, Silex is not tied to Doctrine, and you should be able to use your existing database access code. If you keep your existing system, you'll probably want to write a Silex ServiceProvider to integrate with it.
Next, the hardest part is probably going to be moving your business logic to Silex controllers. One thing to keep in mind is that you don't necessarily need to transition your entire project at once. Depending on how big your project is, you may want to transition a piece at a time.

Related

Laravel Framework php

I've made an online store in php for school, and now my teacher wants this project to have a Laravel Framework. And I have no freaking idea how to do it. Are there any possibilities to implement this framework to my project, or my project to this framework, without starting from scratch? If yes, how should I do it?
irrespective, its going to involve a lot of rework. A lot also comes to down to HOW you've developed your original php app. Laravel is a Model View Controller framework. For starters all your routes (http redirects) are generally managed in a single file (web.php). Your views can be traditional php, however, Laravel gives you a good templating engine called Blade which allows you to shorthand code and keep code a lot cleaner. Models control your table relationships, controllers handle the functions/code/crud etc.
You will love how easy it is in most respects - especially the way eloquent data queries work etc. It can greatly reduce your code.
If your teacher wants you to LEARN Laravel specifically, I would say YES you will be starting over - however, your logic in the code should just need reworking rather than start from scratch.
There are heaps of posts around HOW to install Laravel (apache, virtual box, homestead etc) - once your ready, its super simple to create a new project and start building away... If you are new to MVC, you should do some tutorials first (e.g. laracasts or other).
Best of luck :)

Routing in PHP, and how to set it up

I've recently started work at a new company, and was initially assigned to help the web development team.
To my horror, their template application they use to set up every single one of the projects they do, is completely procedural.
I am very eager to swap it over to OOP, but before I do that I need to actually understand how to create a project.
The initial work is there - there's an index.php that swallows all calls made to the site/web application.
What I need is a tutorial on how to set up proper routing so that I can look at a URL and go "call this function in this controller" instead of including a bunch of PHP files to get the job done.
Could someone please point me in the right direction?
I was in the same situation and this course helps me a lot to learn more about MVC and Routing in PHP. As #Armin said you can use Slim Framework or phroute .In the other hand, you can use Laravel framework to take care not only routing but also all the other challenges as a PHP developer you might have in the future
It sounds like they don't use any well known framework. They probably also don't want to use one. I think the best solution in this case would be some routing library. You could use the Slim Framework. You will understand everything you need to understand in about 30 minutes. It's very easy to setup and easy to extend.
You should not try to write your own routing library, it'll become quickly a quite complex thing. You will just reinvent the wheel and loose time.
I also would recommend the Slim Framework. I've spent way too much time trying to roll my own framework with routing but the maintenance and updating on my own framework got it the way of actual projects.
Slim is extremely easy to understand and offers a lot of flexibility to mold your own type of application. Between the routing, middleware, containers, and using the Eloquent DB ORM it has plenty to offer.
As others mentioned before, don't reinvent the wheel. There are plenty of tools out there you can lean on that allows you to focus on the actual project.
I think that Create your own PHP Framework from Symfony documentation is the thing you are looking for. You shouldn't be worried about the title, it's not only about creating frameworks. It shows you how to start using Symfony components in plain PHP application and eventually create your own framework.
But creating framework part is not the most important one. Understanding how to use object-oriented components in your code is the key part.
Of course, one of the components is routing. It's use is described in first chapters: Introduction, The HttpFoundation Component, The Front Controller, The Routing Component, Templating (don't skip this one!). But I recommend continuing to the end, it's easy to read and very interesting article. Even if you don't want to use Symfony (or it's components) this article will help you understand how to use any modern PHP components.

Symfony: Organising multi-client application’s business logic

Problem:
I’m unsure how to set up multi client application for Symfony so that we would not violate Symfony's best practices and work against the framework.
I would like to have one main Core namespace which would contain all the base model classes. Right next to the core I would like to set up client specific namespaces which would be used, based on client regional setting. For example LocalUS for US market, LocalUK, for UK market etc.
The Local* namespaces should take first priority for including twig templates, and as a fallback use core common shared views (as I understand, this is solvable via twig namespaces). Same goes for controllers and models - these are probably solvable via extending the Core namespaced classes? Is this all solvable via routing and providing paths for these Local* controllers?
I was looking up on github to see if there are any project that have similar setup but I couldn’t find anything.
A little background:
We have an older legacy PHP Application which was built in-house from ground up using plain PHP. As the application has grown over time, it has become hard to maintain good code quality and standards. It’s also very time consuming to teach new developers our application logic, since the application basically follows no standard design patterns and just does it’s own thing. A lot of the underlying code which handles routes, controllers etc seems to work like “magic” that nobody really dares to touch.
It is because of that we would like to migrate our application to Symfony3 framework. I’ve read some articles about the overall process of migrating legacy applications to symfony, and managed to do it with silex pretty well. Silex, however is a bit too lightweight, I found that the assetic service provider had a lot of functionality missing (twig namespacing etc), and decided it would be best if we could move to a full stack symfony framework instead.
Look into Symfony bundles - they do exactly what you need. You create a "base" bundle, than extend it with other bundles. That's how FOSUserBundle works - it provides everything you need, than you extend it and overwrite it.

why do we say ZF2 is a MOVE design pattern? [duplicate]

I'm wondering if ZF2 is based entierly on MOVE instead of the classic MVC.
Somewhere I read that is really bases on MOVE (http://cirw.in/blog/time-to-move-on) but e.g. in the "getting started tutorial" (http://zf2.readthedocs.org/en/latest/user-guide/overview.html) they are saying "creating a simple database driven application using the Model-View-Controller paradigm".
So, what is it now based on? Does it support both?
I'm a bit confused. What are the difference at MVC between ZF1 and ZF2?
Thanks
MOVE (Models, Operations, Views and Events)
MVC (Models, Views, Controllers)
The problem with MVC is that you end up stuffing too much code into controllers,
to overcome this problem MOVE is another possible option to choose in zf2 projects,
Because in this architecture you can split your controller into Events and Operations.
Question: Is ZF2 based entirely on the MOVE architecture?
I don't think ZF2 is designed entirely on MOVE architecture.
ZF2 follows the SOLID object oriented design principle with loosely coupled MVC architecture which provide more flexibility in terms of usability.
Question: Can I develop my projects using the MOVE architecture in ZF2?
Yes ZF2 provides you with everything to support both architectures (MOVE, MVC).
It provides Models, Views, Controllers(Operations), Events.
It is up to the developer which architecture he would like to use.
ZF1 and ZF2 is compared in another thread.
MVC or MOVE
SOLID Object Oriented Programing
I am still a beginner in zf2, I am writing here since I had ported my zf1 code entirely to new zf2 framework. I could only say the new framework have lot of advantages and once you get certain new concepts and feature. you can save lot of time developing new project by reusing of modules/components etc.
In zf2 most of the library components are designed to be standalone, i.e loosely coupled, as said it gives more flexibility and a task can be done in many ways (just like PHP is). The main advantage I see is modules, I can split my project to many modules with ease and test individually and integrate very easily without any additional codes. Even I had all my resources like images, css and JavaScript files inside my module folders and access it without any problem (off course with a small code in index.php)
for me ZF1 has fixed rules, flows, etc, and zf2 have liberated flow and rules with new addition of events namespace, configuration and lazy loading, assemble objects, inject dependencies with servicemanger. in zf1 routes are automatic(programmatic), where as in zf2 we need route definition, in zf1 controllers are automatically choosen, where as in zf2 we need to define controllers as invokables, this are few things that take some time to get in once you get the idea you will feel the comfort of using the new zf2 framework
to start with you can do this steps
Learn Service Manager
How Configuration works
How events work
Components wise more or less it works the same as in zf1 (at least in my case)
below are the following advantage I had benefited
Full feel of OOP
Modular application development
Design and then develop to PHP application
Web Application(RIA) platform

Is ZF2 based on MOVE?

I'm wondering if ZF2 is based entierly on MOVE instead of the classic MVC.
Somewhere I read that is really bases on MOVE (http://cirw.in/blog/time-to-move-on) but e.g. in the "getting started tutorial" (http://zf2.readthedocs.org/en/latest/user-guide/overview.html) they are saying "creating a simple database driven application using the Model-View-Controller paradigm".
So, what is it now based on? Does it support both?
I'm a bit confused. What are the difference at MVC between ZF1 and ZF2?
Thanks
MOVE (Models, Operations, Views and Events)
MVC (Models, Views, Controllers)
The problem with MVC is that you end up stuffing too much code into controllers,
to overcome this problem MOVE is another possible option to choose in zf2 projects,
Because in this architecture you can split your controller into Events and Operations.
Question: Is ZF2 based entirely on the MOVE architecture?
I don't think ZF2 is designed entirely on MOVE architecture.
ZF2 follows the SOLID object oriented design principle with loosely coupled MVC architecture which provide more flexibility in terms of usability.
Question: Can I develop my projects using the MOVE architecture in ZF2?
Yes ZF2 provides you with everything to support both architectures (MOVE, MVC).
It provides Models, Views, Controllers(Operations), Events.
It is up to the developer which architecture he would like to use.
ZF1 and ZF2 is compared in another thread.
MVC or MOVE
SOLID Object Oriented Programing
I am still a beginner in zf2, I am writing here since I had ported my zf1 code entirely to new zf2 framework. I could only say the new framework have lot of advantages and once you get certain new concepts and feature. you can save lot of time developing new project by reusing of modules/components etc.
In zf2 most of the library components are designed to be standalone, i.e loosely coupled, as said it gives more flexibility and a task can be done in many ways (just like PHP is). The main advantage I see is modules, I can split my project to many modules with ease and test individually and integrate very easily without any additional codes. Even I had all my resources like images, css and JavaScript files inside my module folders and access it without any problem (off course with a small code in index.php)
for me ZF1 has fixed rules, flows, etc, and zf2 have liberated flow and rules with new addition of events namespace, configuration and lazy loading, assemble objects, inject dependencies with servicemanger. in zf1 routes are automatic(programmatic), where as in zf2 we need route definition, in zf1 controllers are automatically choosen, where as in zf2 we need to define controllers as invokables, this are few things that take some time to get in once you get the idea you will feel the comfort of using the new zf2 framework
to start with you can do this steps
Learn Service Manager
How Configuration works
How events work
Components wise more or less it works the same as in zf1 (at least in my case)
below are the following advantage I had benefited
Full feel of OOP
Modular application development
Design and then develop to PHP application
Web Application(RIA) platform

Categories