Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jwheat's avatar

Laravel for a part of a larger application

I have a large php app that was not written in Laravel. I need to develop some new functionality and I'm curious if its possible to use Laravel and have it coexist with the rest of the application. Ideally over time the entire app would be rewritten, but that will take a while.

If this is possible, and the routes take over the url processing, how would you access the traditional app urls that aren't setup as routes?

I'm still new at this if you can't tell :) I tried searching before posting but I couldn't figure out the right terms. -Jon

0 likes
32 replies
jlrdw's avatar

how would you access the traditional app urls that aren't setup as routes?

Have a link to them.

beerbuddha's avatar

You have not specify how your application is built. do you have an IoC, how are the routes built, what is your ORM, etc etc

Reasons: Depending on what is there, you can easily swap out certain parts that is "laravel". A lot of parts can be used without laravel. Look for Jeffrey's video on Eloquent and Collections that is separate from Laravel.

From then on, you can sub from there, however, there are a lot of components and structure that cannot be extracted (scheduler for example, or how the queue factory is built, and also php artisan).

jwheat's avatar

Hi @beerbuddha, The original application is a traditional php web application, with no routes, a lot of procedural code, some classes, etc. Thanks for the tip on the video, I'll take a look at that.

leolam2005's avatar

I completely rewrote the raw PHP version into Laravel.

ohffs's avatar

I'm in the (slow) process of migrating an old PHPv4 app to laravel. Largely the process has been integrating some of the framework components (blade, eloquent etc) into the existing codebase to try and strip out all the terrible embedded html, raw SQL queries etc. Separately I've added a new API which is using the router too to bypass the current horrific way it works - so that's pretty much 'pure' laravel.

The plan is that once enough of it is migrated over and is in a testable state I can shift it over to the framework 'proper' without a huge amount of pain. No, don't laugh - it might work! ;-)

I found Matt Stauffer's torch repo really helpful in getting things going.

beerbuddha's avatar

@jwheat what is a traditional web application?

do you mean a website with a landing page like a blog? or what? I'm sorry im trying to make you open up with more detail (unless you have a character limit on your phone / data and can only write under 240 char).

Web application can range in Blog, Ecommerce, CMS...etc Are you using PDO already or not, what kind of legacy PHP are we dealing with? pre-5.4? older? Is it already on nginx or is it on Apache. Does it even touch database or is it a flat file reader. What are you using the PHP part for if its a vanilla PHP...

etc etc etc.

The more details, the more we can say: you can take out this with that.

jlrdw's avatar

I primarily love this forum for two main reasons.

  • it is interestingly funny
  • it is interestingly funny

I guess the

<a href ...

Tag no longer works in laravel.

jwheat's avatar

@ohffs - Sounds like my future :)

@beerbuddha - I didn't want to bore you with all the details, and quite frankly I'm ashamed of the mess of an application I've built, but I've since learned the "right" way. So buckle up, here it comes

This is a volunteer project management application that allows organizations to submit project ideas for review, and post them to a project list, where volunteers can then choose and sign up. The application also allows for those organizations to manage the volunteer data, communicate with them, etc. I've built a form builder which also allows the organization administrator to build custom project submission, and sign up forms so they can collect the necessary data needed. There's a tie in to host and manage clearances by volunteers when they log in, a service "transcript" that keeps track of service hours, and on and on.

What I have to (re)build is the form builder function and I'd like to start using Laravel, but there's no way I can rebuild it all in a timely manner and thought if I could integrate Laravel for this part, and then as @ohffs did, rewrite sections into Laravel.

It is running on its own Amazon EC2 server on GNU/Linux (as uname states), and PHP 5.3.29

It is a traditional PHP application with no routing, or MVC framework at all. I did use class files, but more for organization of functions as opposed to an actual Object Oriented way. A crap-ton of procedural code with HTML scattered about.

All that to say, it runs and runs well, its just a maintenance nightmare, and there's no good way to have another developer help build anything for it.

So there you have it, here's my confession - "I'm Jonathan and I wrote a giant application full of procedural spaghetti code"

jwheat's avatar

@jlrdw - thanks I'm glad I brought a smile to your face.

It may be a stupid question, but with my understanding (or apparent lack thereof) if there is something controlling the routing urls won't that interfere with a url that is not listed in the routes?

For the sake of this (stupid) example, lets say there's 1 route only - /about , so /about hits Laravel as expected.

If in the HTML I link to /projectlist and that is not in the routes, will the project list still load?

That was the heart of the question

jekinney's avatar

As long as the routes don't collide then no issue.

1 like
ohffs's avatar

@jwheat If you're on php 5.3 (so am I...) then you can still pull in the laravel 4.1 components. Having got X% through doing a complete re-write and then realising 'holy cow - this is getting scary' I can only recommend doing it a little at a time. Just using blade is a big win :-)

You can mangle old content/url's - but you're never quite sure you've got them all or that another route won't get added that breaks something in a weird way. If your code is anything like the stuff I'm working in there are hard-coded URL's everywhere so it's a real pain. You could do something like have the new laravel code live in a sub-path like http://blah.com/l/ and then separate things more cleanly - but it's not very nice to deal with.

This talk is worth a watch if you've got time :

https://www.youtube.com/watch?v=65NrzJ_5j58

3 likes
MikeHopley's avatar
Level 17

I second the recommendation from @ohffs to watch the Paul Jones talk.

@jlrdw arch comments aren't doing you any favours.

It may be a stupid question, but with my understanding (or apparent lack thereof) if there is something controlling the routing urls won't that interfere with a url that is not listed in the routes?

For the sake of this (stupid) example, lets say there's 1 route only - /about , so /about hits Laravel as expected.

If in the HTML I link to /projectlist and that is not in the routes, will the project list still load?

It's not a stupid question.

With Laravel or any similar framework, all requests are routed through /index.php. This bootstraps the framework and the framework will then handle the routing. This is done using a rule in the /public/.htaccess Apache settings file:

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

With legacy apps, you might be using the file system directly for your routing. Is this what you're doing? So for example, you add a new page by creating a file like projectList.php. or projectList.html.

You can continue doing this while migrating to Laravel. Just put your legacy web pages in the /public folder. Note, however, that you must link to them with the file extension. So you will need to use <a href="/projectList.php"> (or .html, etc). Linking to /projectList will get "swallowed" by Laravel and result in a 404.

(This could be avoided by changing the .htaccess file, effectively defining legacy routes in there. Probably not much fun doing that).

So you can have some routes "inside" Laravel, and some "outside". The ones outside will need to use the file extension.

Alternatively, you can do a kind of "soft migrate" for your legacy pages. This is what I did. Inside your /resources/views folder, make a new folder called legacy-pages (or whatever). Now you need to make a catch-all route in Laravel:

// routes.php

// Some "proper" laravel routes here
Route::get('/about', 'PagesController@about');

// After everything else, your legacy pages
Route::get('{path?}', 'LegacyPagesController@show')->where('path', '.+');

This means that the LegacyPagesController will receive all undefined routes:

// LegacyPagesController

public function show($path)
{
    $view = 'legacy-pages/' . $path;

    if ( View::exists($view) )
    {
        return view($view) );
    }
    App::abort(404);
}

So if the page exists, it will be used. Otherwise you will get the 404 page.

Note this exact code may need some tweaking as I haven't tested it. The principle works, though; I am using this on my site for legacy content.

This was one of the things I got really stumped by when I was learning Laravel -- because of course, all the tutorials are about nice, sane, clean code, not digging yourself out a pit of legacy code despair. ;)

3 likes
jlrdw's avatar

@MikeHopley you are way over thinking the simple. Say I was using laravel for some new stuff, but had some old stuff at whatever_site.com I could simply link to it, and press right on. It is amazing how some people will take the simplest easiest little to do thing, work very hard to make it a complex thing.

ohffs's avatar

@MikeHopley

The principle works, though; I am using this on my site for legacy content.

Can we swap legacy codebases? Pretty please? :: flutters eyelashes :: ;-)

ohffs's avatar

@jlrdw I think you're way, way underestimating how tricky mixing terrible legacy code into another framework is ;-)

jlrdw's avatar

ANd @MikeHopley I did not say

It may be a stupid question, but with my understanding (or apparent lack thereof) if there is something controlling the routing urls won't that interfere with a url that is not listed in the routes?

For the sake of this (stupid) example, lets say there's 1 route only - /about , so /about hits Laravel as expected.

If in the HTML I link to /projectlist and that is not in the routes, will the project list still load?  

OP did, so I have no idea what you are trying to do here.

MikeHopley's avatar

@ohffs Not a cat's chance in hell. I've seen enough of your Pandora's box to give me nightmares. ;) You're braver than I.

Say I was using laravel for some new stuff, but had some old stuff at whatever_site.com I could simply link to it, and press right on.

Have you considered that I might want both old and new stuff under the same domain? There are technical as well as "sentimental" reasons that one might prefer this. For example, if users have session data that is shared across the old and new, or simply for easier analytics.

With that said, it's a good suggestion with a bit of lateral thinking. Maybe you could have offered it to the OP when you thought of it?

A piece of advice: don't rush to assume everyone else is dumb, and don't act superior. It neither convinces nor endears.

so I have no idea what you are trying to do here.

I primarily love this forum for two main reasons.

* People are helpful
* People are kind, not snide

Let's keep it that way.

jlrdw's avatar

@jwheat all I was trying to say is you can continue to work on old while building to new app by simple linking to it, but as you can see some people have to blow things out of proportion. Not @ohffs his answers are good, but there's another.

jlrdw's avatar

I think you're way, way underestimating how tricky mixing terrible legacy code into another framework is

Please tell me you are not that stupid, Duh, I am referring to only still working with old while building new. You cannot possibly be that freaking stupid to think that I would think that way. You freaking people have taken a rather simple solution and twisted to hell.

I never never never never said freaking mix old code in new laravel, I freaking said LINK to it so you can open old app and work on it if needed, some freaking people love termoil I guess.

ohffs's avatar

@jlrdw you're coming across as very insulting :-/

Please tell me you are not that stupid, Duh

You cannot possibly be that freaking stupid to think that I would think that way.

You freaking people have taken a rather simple solution and twisted to hell.

some freaking people love termoil I guess.

@jwheat the laracasts forum isn't normally abusive like this - I hope you got some useful info from the thread :-) Goodnight from me in the meantime.

jlrdw's avatar

You people took my answer and twisted it into something it wasn't, so I guess I am suppose to just smile.

I think you're way, way underestimating how tricky mixing terrible legacy code into another framework is

I never thought that, you said I thought that. I DID NOT HAVE THAT THOUGHT, WHY WOULD YOU SAY I THOUGHT THAT WHEN I DIDN'T? ISN'T THAT NASTY PLANTING A THOUGHT FOR ME, BUT I NEVER THOUGHT THAT SO YOU WERE THE MEAN NASTY PERSON FIRST.

MikeHopley's avatar

@jlrdw So many of your posts are aggressive or trying to show off. This may be normal for other forums but not for Laracasts, which is why these forums are so good.

People will respond better to your expertise if you present it in a friendly way. For example, don't talk to people as though you think they're stupid. It never gets a good reaction.

...unless perhaps you are looking for a bad reaction?

One other suggestion. You say that you have been misunderstood and misinterpreted -- and I wouldn't be surprised if you're right. So now think about it the other way around: isn't it likely that you also misunderstand and misinterpret other people sometimes? Maybe people are not as dumb as you think.

1 like
jlrdw's avatar

OP ask

how would you access the traditional app urls that aren't setup as routes?

I gave a simple answer but you people wanted to pick a fight with me for some reason.

jwheat's avatar

Thanks to everyone that responded, I think I feel a bit better about the prospect of taking this on. Granted, I wrote the crap code, so even if I can't get Laravel to work, I can maintain it.

Thanks @beerbuddha for making me come to terms with my garbage code publicly :) Thanks @ohffs I'll definitely watch that video and start playing around with this. Thanks @MikeHopley for that explanation, at least I'll have a better understanding on what I'm doing I hope. Thanks @ohffs - at least I know I'm not alone, and its all good, the forums rock as does Laracasts :) Thanks @jlrdw for keeping it real simple

I'm off to start the process...

MikeHopley's avatar

@jlrdw I'm quoting this in case you decide to change it later:

You are the dumbest mf in the world, The links to old is only while new is being built, eventually all would be new. You are one dumb mf. People like you disgust me, and make my skin crawl. You have insulted me to no end by taking an answer I tryed to help with and turning it to something it is not. I want to talk to you in person.

This is an abusive and threatening post. There is no excuse for that kind of behaviour. Grow up.

1 like
Next

Please or to participate in this conversation.