PHP Reddit – Telegram
PHP Reddit
34 subscribers
289 photos
37 videos
24.8K links
Channel to sync with /r/PHP /r/Laravel /r/Symfony. Powered by awesome @r_channels and @reddit2telegram
Download Telegram
How do you handle exceptions which you expect not to be thrown?

This question bugs me for a long time.
Often times an exception could be theoretically thrown but this will never happen in practice because the codepath is basically static.
Thats also wouldnt be a Problem if IDEs like PHPStorm wouldnt warn you about every unhandled exception through the complete stack trace.

So what patterns are you using to handle stuff like `DateMalformedException`, `RandomException` etc. which you no wont throw and if so it should crash.

For example given the following method:

/**
* @return $this
* @noinspection PhpDocMissingThrowsInspection // if removed doccomment also triggers warning
*/
public function expire(): self
{
$this->expirationDate = new DateTime();
$this->expirationDate->modify("-1 day"); // Unhandled \DateMalformedStringException

return $this;
}

Because the values are static you can expect that it works. More dynamic example would be:

function addDays(DateTime $date, int $days): DateTime
{
$date = clone $date;
$date->modify("+$days days"); // Even in this case we can safely assume the format is always correct because days is an int.
return $date;
}

Another candidate is `random_int`

random_int(1, 10); // Unhandled \Random\RandomException

https://redd.it/1mk3xzd
@r_php
Newbie learning to educate myself

Hey everyone!

I'm a bit new to the programming world (besides a few classes in early college years) and was wanting to start my journey into programming with PHP.

Why?
Well, I work closely with the owners of a photography business who commissioned a photo album app with compression and all this other stuff from someone on Upwork but, none of them really know the behind the scenes as it relates to the code.

It's still in development but what they're using is SQL, cloudflare R2, and laravel so I was hoping to get educated enough to at least have some degree of experience with keeping things in line (long, long road from doing anything but, it at least would give us some understanding of what we're looking at besides just being the just app users).

So, my question is, is there a best way to help educate myself on these topics to be somewhat competent and help out once the app is complete? I know it will be a long road, but I'm up for it!

Thanks!

https://redd.it/1mk6ggp
@r_php
Camel case vs snake case inconsistency

How do you guys deal with camelCase and snake_case inconsistencies?
I'm particularly interested for object properties. I know the usual suggested way is camelCase, but when your db columns are in snake case it can get a bit confusing to see db queries with snake_case column names (also array indexes), but then use camelCase when accessing it as an attribute of the object. Similarly a lot of api objects use snake_case as well...

I'm curious how others deal with this

https://redd.it/1mk92hm
@r_php
File-based Routing Microframework Based on HttpKernel
https://zack.tebe.ch/

https://redd.it/1mkk412
@r_php
🌍 GeoIP Bundle by ndevs.eu

🆕 New Symfony Bundle just dropped!


Detect geolocation from IP address with ease 🚀
Supports MaxMind & IP2Location (with optional fallback)
✔️ Clean config
✔️ Auto-injected into request
✔️ Perfect for CDNs, proxies & mock IPs in dev

📦 composer require ndevs-eu/geo-ip-bundle


🔗 https://github.com/ndevs-eu/geo-ip-bundle

Open-source & production-ready 💪
\#symfony #php #opensource #devtools #geoip #ndevs

https://redd.it/1mkqowy
@r_php
Symfony LiveProp and mapping adjusted ManyToMany collection.

Howdy,

I've been experimenting with Symfony and UX LiveComponents + Mercure in order to create a digital character sheet for my roleplaying group. By combining LiveComponent events and listening for updates via Mercure I managed to get Real-Time updates between two sessions to work.

My initial test was a simple string value stored for each character to represent their name, and I instantly hit a snag when trying to use a more complex variable as a LiveProp.

Each character has a variable amount of attributes assigned to them (since not every character has every attribute), so my Schema looks like this:

Character (id, name)

Attribute (id, name)

Character_Attribute (id, character_id, attribute_id, value_current, value_max)

The reason I call it an Adjusted ManyToMany is because each attribute has a current and max integer value that is unique to each character, so instead of using the ManyToMany doctrine mapping I've created a separate entity called CharacterAttribute which is mapped ManyToOne Character and ManyToOne Attribute.

So Symfony sees the following:

CharacterAttribute.php
    #ORM\ManyToOne(inversedBy: 'Attributes')
    #ORM\JoinColumn(nullable: false)
    private ?Character $character = null;

    #ORM\ManyToOne
    #ORM\JoinColumn(nullable: false)
    private ?Attribute $attribute = null;
-------------------------------------------------------
Character.php
/
@var Collection<int, CharacterAttribute>
/
#ORM\OneToMany(targetEntity: CharacterAttribute::class, mappedBy: 'character', orphanRemoval: true)
private Collection $Attributes;

I pass a Character variable to a LiveComponent twig.php-file where it is listed as a #[LiveProp(writable:['name'\])\]

I can access the PersistentCollection of attributes for each character without issue in for example twig-templates by looping over Character.Attributes, but here are the issues I have encountered.

Test 1: LiveProp writable attribute

If I add the Attributes property of the Character Entity to the #[LiveProp(writable:['name', 'Attributes'])] attribute that is assigned to a Character variable in the twig.php-file I get the following error:


An exception has been thrown during the rendering of a template ("The writable path "Attributes" on the "character" property of the "App\Twig\Components\Character\GetAttributes" component must be a scalar or array of scalars.")

I assume since the Attributes property is a Collection<int, Character_Attribute> that is is too complex to dehydrate.

Test 2: Free-standing LiveProp

If I add the CharacterAttributes entity as its own variable to the twig.php-file like this:

#[LiveProp(writable:true)]
public CharacterAttributes $attributes;

Then I get this error message:

An exception has been thrown during the rendering of a template ("Expected argument of type "App\Entity\CharacterAttributes", "Doctrine\ORM\PersistentCollection" given at property path "attributes".")

So I change the variable type to PersistentCollection instead.

An exception has been thrown during the rendering of a template ("The "owner" property on component "App\Twig\Components\Character\GetAttributes" is missing its property-type. Add the "App\Entity\Character" type so the object can be hydrated later.")

Test 3: LiveProp writable(true)

I tested changing the Character #[LiveProp\] attribute from

#[LiveProp(writable['name'])

to

#[LiveProp(writable:true)]

To make the entire Character entity writable. I don't get an error message this time and I can even access the Attributes property in my Twig-component by writing: {{ Character.Attributes }}

I could even loop through everything but I have been unable to map the individual attributes inside the Attributes variable to Live inputs. For example, in the following code I can access the valueCurrent property from attribute which comes from {% for attribute in this.Character.Attributes %} and
output it, but when I do this, I cannot update any property on the Entity (not even the name property I could edit before).

<div>
{{ attribute.valueCurrent }}
<input data-model="on(change)|attribute.valueCurrent" data-action="change->live#action" data-live-action-param="saveChanges">
</div>

Now I think I know why this is, and that is because there is no LiveProp-ed variable in the twig.php-file matching the name "attribute". Is it possible to edit individual entities inside a LiveProp-ed collection?

Now

This is where I've hit a dead-end. In addition to above I've tried to create a DTO-class to hold the data, but got the same error message as Test 2. I've tried to hydrate/dehydrate with custom functions, and I managed to get it to dehydrate, but found no way to rehydrate the values back to the Character entity.

So my question is has anyone here tried to use LiveProp/LiveComponents with "complex" entities like this before?

Is it even possible to use LiveProp with a PersistentCollection like this or should I resign myself to using something like UX Turbo and forms?

https://redd.it/1mkqwji
@r_php
Filament v4 is launching on Tuesday, August 12th!

The FilamentPHP team [announced the release date](https://x.com/filamentphp/status/1953574144098127972) for Filament v4 yesterday. Lots of meaningful improvements for performance, DX, and customization. You should check out the [great overview](https://filamentphp.com/content/leandrocfe-filament-v4-beta-feature-overview) posted by Leandro Ferreira but a few highlights of v4 are:

* Performance: Large tables render 2–3x faster.
* Tailwind v4: Modernized theming with oklch colors.
* Auth: Built-in MFA (TOTP apps + email codes).
* Resources: Nested resources and better organization.
* Forms: New TipTap rich editor, slider, code editor, table repeater, partial rendering, and JS hooks to reduce network requests.
* Tables: Custom data sources (no DB required), better bulk actions, reorderable columns.
* Actions: Unified actions across tables/forms/infolists with rate limiting and better testing.
* Panels: Strict authorization mode, local Inter font, improved error notifications.
* Theming: CSS improvements have made theming significantly easier. There are great themes available at [https://filamentthemes.com/](https://filamentthemes.com/) and [https://filafly.com/](https://filafly.com/) as well as many other community options being released on Discord or the [Filament Plugin](https://filamentphp.com/plugins) page.

What feature are you most excited to try first? Are you planning to upgrade right away or wait for a while post launch?

https://redd.it/1mkvmg7
@r_php
insight about my portfolio

Hello everyone!

so i've been learning and learning from online resources and with aid of various LLM's php/laravel/mysql/js/react/docker, and i've managed to get by into doing a sort of self-assessment/hands on learning projects that i thought would be helpful with landing me an entry level/junior position anywhere remotely, but it seems like i keep getting rejected over and over, and im not sure if the market expects something more or something else entirely, i tried to create a couple of projects that demonstrates my level of knowledge at this point, my GH here has them: https://github.com/Abdu-Ahmed ,,, am i doing this wrong? should i pause the job hunting and work on a specific aspect? im not sure and quite frankly i feel lost, any insight and or advice is much needed.

Thank you!

P.S i do NOT have any relevant work exp and a drop out so yeah, you can guess how difficult it is :/

https://redd.it/1mkwa7w
@r_php
A simple, lightweight PHP framework - Swidly

I've built a lightweight PHP framework that runs straight out of the box—no Composer, no downloads, no third-party dependencies.

tiger2380/Swidly-MVC-Framework: 'Not Another' PHP MVC

I get it—another PHP MVC, really? But Swidly isn’t trying to reinvent the wheel. It’s designed to strip away the clutter and complexity with a clean, intuitive routing system. No steep learning curve. No bloated setup. Just simple, functional code that gets you moving fast.

If you hit any snags or have feature ideas, I’d love to hear from you.

Thanks!

https://redd.it/1mkwpyp
@r_php
How to participate in RFC?

The php 9.0 RFC is ongoing and without a date yet. I've been hopping, and would like to suggest, that support for typed array or collections are added to the return value of methods. Right now, one can only "hint" it with comments on the method e.g. @returns MyClass.
Of course I can add my own classes to do the work, but given that the use case is so commonI think it is a feature that would enhance the language, make it more predictable and reduce errors.

So... Do you guys know where/ how and to who i could suggest this enhancement, if possible at all?

https://redd.it/1mlmywe
@r_php
Laravel site apparently hacked

Hi. I have a problem with my personal site on Laravel and Php. My table user is filled with data I don't know.
My web app in Laravel doesn't have an option to add users so I don't know how the attacker add data to user table.
Where can I check on my Laravel app?

My server is Digital.ocean.
My db is MariaDb and only listen on localhost.
The server access is only with ssh password that I only know.
I changed my user ssh password the month before, but yesterday again my user table is filled with strange data.

Thanks.

https://redd.it/1mlyxaf
@r_php
How do you use twig live components?

I really like using Twig Live Components to make a page interactive, so my whole page is a Live Component. This makes communication between properties and updating state really easy.
But is this a misuse of Live Components, since they are typically meant to be smaller components that compose the page, not the whole page itself? So currently in my Project the twig Template rendered by the controller is just a wrapper for one twig live component that contains all the HTML separated by includes.

I tried separating one page into smaller Live Components, but the overhead of adding so many events for communication between them just doesn’t seem worth it to me.
I just want to hear your opinion on how you use Live Components and how they should be used.

https://redd.it/1mm31b5
@r_php
What is your opinion about Ziggy in Interia applications?

I have started developing an application using Laravel and InertiaJS a few months ago. At this time I bootstrapped the project with one of the Laravel starter templates. By default this templates come with Ziggy preinstalled. My first thought was: cool feature, so I don't have to reference the paths directly in the client-side navigation, but can fall back on the route names.

As the application has grown and more and more routes have been added, I have become increasingly concerned about performance and security. Each Interia Response contains a ziggy object with all routes of my application.

The object includes routes to sensitive parts of the application like admin area, horizon etc. These routes are specially secured, but I still think that not every user should know about them.
Due to the growing number of routes, the Ziggy object is currently 170kb in size. This means that every Interia Response is 170kb larger than it needs to be. I think that even with a small number of users, this quickly adds up.

What is your opinion on this? Do you still use Ziggy despite these drawbacks?

https://redd.it/1mmfekn
@r_php
Weekly /r/Laravel Help Thread

Ask your Laravel help questions here. To improve your chances of getting an answer from the community, here are some tips:

What steps have you taken so far?
What have you tried from the documentation?
Did you provide any error messages you are getting?
Are you able to provide instructions to replicate the issue?
Did you provide a code example?
Please don't post a screenshot of your code. Use the code block in the Reddit text editor and ensure it's formatted correctly.

For more immediate support, you can ask in the official Laravel Discord.

Thanks and welcome to the r/Laravel community!

https://redd.it/1mmnsnr
@r_php