Jump to content

PHP Frameworks for state


Adamhumbug

Recommended Posts

Hi All,

I have been writing raw PHP for some time and have been enjoying it - i am an ethusiastic beginner but have built some cool stuff and write fairly regularly as a hobby.As

One thing that gets a little bit tiresome occasionally is:

I build a nice UI that submits to the database.  I then have to write a lot more code to also handle existing data.

So... i create an add user page that looks great, adds dynamic content and saves to the DB.  Then, when i want to edit that user, i have to rebuld what i have done to take into account that there may be a user that i am editing rather than creating a new.  This often involves a lot of new code and refactoring of what i have done.

Now i understand that that is probably the nature of the beast but i am wondering if i should be exploring some frameworks (maybe laravel) as a next step progression or wheter i should be looking to move into htmx or javascript and my front and back end.

As i said above, i am an amatuer and have never done this professionally so am not aware of any "standards" and would love some experienced feedback on my options.

 

TLDR;

I want a faster way of handling both new data and editing data when it comes to building UI.

I am thinking about what is next in my coding journey.

 

 

As always, thanks in advance.

 

Adam

Link to comment
Share on other sites

perhaps you have seen my suggested page layout posted in the forum? code for any page should be laid out in this general order -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce data needed to display the page
  4. html document

when editing existing data, you would test for a requested edit id $_GET input in the get method business logic, if the form has never been submitted, to query for and fetch the existing data to be edited. you would fetch the data into a common array variable that also holds a trimmed working copy of the form data that gets set inside the post method form processing. this common array variable being empty or not is how you determine if the form has never been submitted (requires at least one valid $_POST field in the form.)

by using a little conditional logic in the form code to control a hidden field value indicating which post action (create/update) to preform, a hidden field with any requested edit id, and switch/case logic in the post method form processing to control which form processing code to execute, you can (re)use the same code for creating new entries or updating existing data.

also, once you have more then 2-3 form fields you need to dynamically validate and process the data and dynamically produce the form fields by creating a defining array with the expected fields, along with their field/data type, select/checkbox/radio choices, validation rules, processing rules, and any other unique values that define each field. you would then loop over this defining data to validate, process, and produce the form fields, instead of writing out code for every field.

Link to comment
Share on other sites

Using a framework like Laravel or CodeIgniter will cut down on the boilerplate code you're creating, but you still need to code. So while in vanilla PHP you'll write the DB connection script once then include it in an object and use that to persist the data to the database, with a framework you'll still be doing the same basic thing. The only difference is that someone has already written the DB connection for you - you still need to assign the values to the columns and tell the framework to save the record.

Where frameworks really come in handy is convenience and convention. Most frameworks that I know of these days are OOP-based so they're using either PDO or MySQLi behind the scenes, and most will parse the insert/select queries you pass to them into prepared statements. This is obviously convenient because you don't have to specifically write that code. It's still very possible to screw it up and bypass it, but you kinda have to try to do it. The convention part is less obvious and arguably less ... good, I guess. Frameworks have their own dialect of PHP and it's not always easy to learn or keep track of. On top of that, there's a lot of magic some frameworks (cough cough ... Laravel) inject that can make it difficult at times to reason about the path the data takes through your system. Once you learn the dialect you can get things done quite quickly, but sometimes that dialect changes in an update and that can make life difficult. Again, Laravel was most notorious for just changing major things in minor updates but they've lately stopped doing that and the upgrade path is much easier and safer than it was just a few years ago. That having been said, some of the major updates can still be rather jarring.

I'm not a blind fan of Laravel, but I've been using it for about 5 years now almost exclusively and I've gotten used to it and find it enjoyable for the most part. It offers a lot by default and once you get used to the syntax, service layer, and some of the other magic involved it's possible to get a lot done quickly. I've also worked with and enjoyed CodeIgniter 4 but it offers less built-in functionality and magic out of the box (or at least it did at the time). Yii sucks in my experience and opinion, but YMMV.

Oh, and obviously there's a learning curve with a framework - regardless of which you choose.

Edited by maxxd
Link to comment
Share on other sites

For me, personally, I am not a fan of frameworks at all and mostly enjoy raw coding more. However, I have used CodeIgniter for a couple of projects and enjoyed it. Massive learning curve but got the results I was after. I still go back to and prefer raw coding.

What I have done is, I created an application and used the UI for future projects. So, for example, login, register, users etc, all from the same UI. All I would need to change is the colours or fonts? Saves me a lot of time and effort.

Link to comment
Share on other sites

Yeah, I had a fairly robust component library before I ended up at jobs that used or were moving to Laravel - it's a good way to go whether you're using a framework or not. And it's always possible to use packages as bits and pieces in your own home-grown PHP code. Symfony is modular and it's easy to use composer install to use the packages you're interested in. Same with some of the Laravel packages, though I think that's probably more difficult as there's a lot of inheritance in Laravel and that could cause some issues.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.