Laravel Framework php - 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 :)

Related

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.

Structure of legacy project with Silex

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.

Admin Routes (Nested Controllers or Bundles)

I'm studying the Laravel 3, 1 week ago, but didn't understand everything about the routes.
My main question is: how to create administrative routes?
In the video lessons from Jeffrey Way (Tuts Premium), I could understand two things about it:
Nested Controllers (/application/controllers/admin/user.php)
Bundles (/bundles/user.php) - He did not say much about it.
Anyway, I noticed 2 things (obvious):
On both sides, I can have a route / admin / whatever.
But what the correct way?
I'm really very confused.
Laravel bundles are for developing modular code that you can reuse from application to application in Laravel. The Bundle itself is very much the same as the 'application' directory you have as standard in a Laravel install, allowing you to create modular sub applications within your project. I highly recommend you avoid bundles for the moment entirely and focus on learning the core functionality of Laravel.
For your needs, place your routes within your routes.php file within the application directory and nest them to your hearts content. This will serve your purposes fine. If you're not building/using bundles, you don't need to use bundle routes.
When you're comfortable with Laravels routing and you've built one or two apps you may well have an idea for a bundle that will help you develop your apps faster in the future. This is the time to start learning about bundle routing as it's the only way to link your application logic with your bundle and provide it with a URL schema.
Hope that helps.
Neither way is really right or wrong, the beauty of Laravel is that there are so many ways to achieve the same thing so it's up to the developer to choose what works for them.
Personally I started by using nested controllers as they're much easier to get up and running. I would however recommend making the move to bundles. If you plan on sticking with Laravel (and you should) then it would make sense to build a bundle that includes the auth and components you use in each project already setup. That way you just need install the bundle and you're good to go.

What is a good PHP framework for migrating an existing app to?

I've recently come on board with a PHP application. There has not been much design put into the app - there is a front controller, however, view, model and controller code is usually mixed together.
I would like to slowly start refactoring the code beginning with moving all the SQL queries into appropriate models. Having looked at a few frameworks, it seems that most frameworks are setup with the assumption that an app will be developed from scratch using that framework.
Is there a good framework that I can just kind of plug-in so that I can use the model libraries and then perhaps later on, integrate more of the app into the framework? Would Doctrine be a good tool for something like this? Or should I just setup my own models without relying on third-party libraries?
If you have an existing php application, then there is no point to migrate it to some framework. Frameworks help you to develop project faster - you already have one. At this point adding framework on top of what you have will only make your application more complex and, most likely, slower.
Instead of trying to find some magical framework, you should invest in refactoring (apply some OOP principles, like SOLID) and documenting the existing code. Maybe start incrementally add some unit-tests.
P.S. Doctrine is an ORM ( 1.x implements activerectord , 2.x - something like a datamapper ). ORMs are for people who are extremely lazy, misguided or do not understand SQL. By moving your existing SQL queries to ORMs code you will make it all only slower. Additionally, you will encounter problems when migrating complicated queries. Just dont do it.
I've just started rewriting a site in Kohana, and I'm quite happy with it. It seems flexible and powerful. It's very similar to CodeIgniter, but a little more modern (more of the latest PHP features are used).
If you want only MVC (not HMVC or more difficult) frameworks for simple adoptation with existing application you should touch CodeIgniter. ActiveRecords is very easy for refactoring from existing sql-queries. You even can use default or hand made functions or classes for building queries in its models.
But default, Codeigniter isn't enough flexible for difficult or multi-namespacing applications.
Im migrating a site using Zend and Doctrine2.
The site wasn't OOP in any sense but it was divided between logic view and dal. The migration is still in process (new features are coded with framework, existing feature gets migrated only when big changes are needed).
Both frameworks are non intrusive, and the most valuable thing you get for me is to standarize things.

What are methods for migrating from one technology to another?

I'm working on a project using Zend Framework, which I initially chose since I had a bit more experience in it. At this point in time, I'm thinking of moving to Django, though, as our current team will be more productive in it.
I'm trying to figure out the best way to (potentially) go about this, as we evaluate it. A lot of our code is still fairly fluid, and being re-written fairly often. The way I had considered is that as something ends up being re-done, redo it in Django, and change my PHP model to simply be a call to the Django underbelly and translation to PHP. Over time, we should have all of the models, at least, in Django.
Are there better ways? Particularly, is there any way to easily manage having two codebases at once?
There is no generic answer to cross-language migration projects.
If you can wrap your php code and hide it behind web services, you will be on a good path because you will be able to migrate those web services later without changing the API.
Make a list of the main components of your architecture and try to see which ones could be hidden behind such a web service API.
I would recommend that if your web app is db driven, move your business logic to your db and have your data mappers call db stored procedures. I'm developing using ZF myself, which is a very strong tool, but the immense 'abstracticity' it offers can deviate you from your goals... Then you can mess as much as you like with your controllers and views...

Categories