0

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?

Skytiger
  • 1,745
  • 4
  • 26
  • 53
  • 3
    You might want to look at working it in to a framework like Laravel instead which has routing built in. – Difster Jul 13 '17 at 21:55
  • Take a look here: https://stackoverflow.com/questions/15392024/is-there-an-standalone-php-routing-library But if you're going to migrate the code, may worth read about PHP frameworks and architectures to choose what best fit for your purposes. – Hugo Dias Jul 13 '17 at 22:03
  • Thanks all, I was actually already considering using either Laravel or Zend 3.0, but I really wanted to get to understand routing better before jumping into a framework :) – Skytiger Jul 16 '17 at 21:19
  • "To my horror, company I just joined is doing X. I am very eager to do Y." You may be right, but be careful not to [tear down Chesterton's Fence too soon](https://grugbrain.dev/#grug-on-chestertons-fence). – ggorlen May 14 '23 at 16:03
  • @ggorlen Thanks for the response, I've since moved on from that company. We did end up changing the way things where done, and I didn't just dive in and start changing things without doing some research - their reason was simple "because that's how we've been doing it for the past decade" (I'm paraphrasing, but that was the gist of it) – Skytiger May 21 '23 at 23:10

4 Answers4

3

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

Jeff
  • 380
  • 7
  • 20
2

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 lose time.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Armin Šupuk
  • 809
  • 1
  • 9
  • 19
  • Thanks Armin, I was actually looking at Zend or Laravel. They are not using any type of framework at the moment, and it's annoying to find your way through the code. – Skytiger Jul 16 '17 at 21:20
  • You could start a discussion within your team with the goal to find a good framework for the whole team. Maybe the management is willing to pay some schooling for the whole team. Just argue that it would incredibly increase the productivity. Whatever happens, good luck! – Armin Šupuk Jul 16 '17 at 22:39
  • They do seem open to the idea, but currently I'm very new and want to get through my probation period before making big suggestions like that :) – Skytiger Jul 17 '17 at 02:38
2

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.

Community
  • 1
  • 1
Rob
  • 1,840
  • 2
  • 12
  • 19
1

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.

Michał Pipa
  • 562
  • 3
  • 10