PHP Reddit – Telegram
PHP Reddit
34 subscribers
294 photos
39 videos
25K links
Channel to sync with /r/PHP /r/Laravel /r/Symfony. Powered by awesome @r_channels and @reddit2telegram
Download Telegram
Sitemap Generator in Laravel.

Hello guys. I have code like that:

<?php


namespace App\Console\Commands;


use Illuminate\Console\Command;
use App\Services\Sitemap\SitemapService;


class GenerateSitemap extends Command
{
    protected
$signature
= 'sitemap:generate';
    protected
$denoscription
= 'Generate sitemap.xml including news';


    public function handle(SitemapService
$service
)
    {
       
$path
= public_path('sitemap.xml');
       
       
$service
->generate(
$path
);


       
$this
->info("Sitemap generated: {
$path
}");
    }
}

<?php


declare(strict_types=1);


namespace App\Services\Sitemap;


use Carbon\Carbon;
use SimpleXMLElement;
use Illuminate\Support\Facades\File;


class SitemapService
{
    private const BASE_URL = 'https://example.com';


    public function generate(string
$outputPath
): void
    {
       
$xml
= new SimpleXMLElement(
            '<?xml version="1.0" encoding="UTF-8"?>
            <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'
        );


       
$staticUrls
= [
            ['/', 'daily', '1.0'],
            ['/section-a', 'daily', '0.9'],
            ['/section-b', 'hourly', '0.9'],
            ['/section-c', 'weekly', '0.8'],
            ['/section-d', 'daily', '0.8'],
            ['/about', 'monthly', '0.5'],
            ['/contact', 'monthly', '0.5'],
        ];


        foreach (
$staticUrls
as [
$path
,
$freq
,
$priority
]) {
           
$this
->addUrl(
               
$xml
,
                self::BASE_URL .
$path
,
                now(),
               
$freq
,
               
$priority
            );
        }


        Model::where('status', 1)
            ->whereNotNull('published_at')
            ->latest('published_at')
            ->chunk(500, function (
$items
) use (
$xml
) {
                foreach (
$items
as
$item
) {
                   
$this
->addUrl(
                       
$xml
,
                        self::BASE_URL . '/content/' .
$item
->
slug
,
                       
$item
->
published_at
,
                        'daily',
                        '0.8'
                    );
                }
            });


        File::put(
$outputPath
,
$xml
->asXML());
    }


    private function addUrl(
        SimpleXMLElement
$xml
,
        string
$loc
,
        mixed
$lastmod
,
        string
$freq
,
        string
$priority
    ): void {
       
$url
=
$xml
->addChild('url');
       
$url
->addChild('loc', htmlspecialchars(
$loc
, ENT_XML1));
       
$url
->addChild('lastmod', Carbon::parse(
$lastmod
)->toDateString());
       
$url
->addChild('changefreq',
$freq
);
       
$url
->addChild('priority',
$priority
);
    }
}

Schedule::command('sitemap:generate')
    ->hourly()
    ->name('hourly-sitemap-refresh');

It generate sitemap.xml automatically. And update it in one hour right? Now is there is a way that ping it to search engines automatically? and how can I check it works correctly?

https://redd.it/1qch9lu
@r_php
Messenger not consuming after Postgres upgrade

This is a bit of a long shot. Anyone have messenger not consume messages after a Postgres/SSL upgrade on Linux?

I've had a couple completely separate projects on separate VMs (zero commonality) log an error like this:

console.CRITICAL: Error thrown while running command "messenger:consume async --time-limit=3600". Message: "An exception occurred while executing a query: SQLSTATEHY000: General error: 7 FATAL: terminating connection due to administrator command SSL connection has been closed unexpectedly"


I've tracked the time the error starts to the EXACT time that Postgres is upgraded (via automatic upgrades). Messenger never recovers - it'll just log errors for days.

Messenger is running via Supervisor

Is this a known issue and/or is there a fix on the symfony side of things? I can work around it on the linux update schedule, but would prefer Messenger be able to recover on its own.

Cheers!

https://redd.it/1qcuyez
@r_php
Laravel Community Suspended on X

I just noticed that Laravel’s official X community was suspended. At first, I thought this was something wrong on my end, but it seems to be a global suspension. I’m not very into social media, but X and Reddit official communities are my favourites and the ones I’m most engaged with. I don't know the reason it had been decent to me, this feels like it could have a quite negative effect on Laravel itself.

https://redd.it/1qd9wg6
@r_php
Current state of end to end testing frameworks for a vanilla PHP codebase

I'm currently upgrading a legacy vanilla php 5 codebase to PHP 8 and refactoring the structure of the code around more of a MVC pattern (rather than the pure functional approach it originally had). With this, there is a lot of code being moved around and I'd like to create some tests to ensure certain functionality appears to work.

What is the most common/most used e2e testing framework for PHP applications these days? Playwright? Codeception? Selenium? Others?

https://redd.it/1qdavsi
@r_php
A free Shift to configure "Fast Laravel"

After implementing the strategies on a few different Laravel projects, I got tired of copy/pasting a bunch of snippets and files.

I figured I'd automate the tedium with a Shift... So, allow me to introduce the "Fast Laravel" Shift.

This free Shift configures a new, separate static middleware group (discussed in this Laravel News article) and adds two custom middleware for page caching.

These strategies (and more) are demonstrated in my "Fast Laravel" video course. If you want to learn more about the course and benefits of caching, there was a good Reddit discussion earlier this month.

https://redd.it/1qdiyb5
@r_php
Convert vardump output to PHPStan array shapes - Hell2Shape

Hi folks! Made a CLI tool that converts var\
dump output into PHPStan type annotations. Check it out: https://github.com/Feolius/hell2shape

There's also a web version, if you want to try it without installing anything (see repo docs). Works locally in your browser without sending any data to server (thanks to php-wasm).

Useful when you need to type those messy arrays and stdClass objects, but can't be bothered to do it by hand. It's not designed to be perfect, but it provides a solid baseline for manual refinement.

Feedbacks welcome!





https://redd.it/1qdlox5
@r_php
AI generated content posts

A bit of a meta post, but /u/brendt_gd, could we please get an "AI" flair that must be added to every post that predominantly showcases AI generated content?

We get so many of these posts lately and it's just stupid. I haven't signed up to drown in AI slop. If the posters can't bother to put in any effort of their own, why would I want to waste my time with it? It's taking away from posts with actual substance.

For what it's worth, I'm personally in favour of banning slop posts under "low effort" content, but with a flair people could choose if they want to see that garbage.

https://redd.it/1qdrv9c
@r_php
Coupled vs Decoupled

What’s your approach if you’re a solo dev and you’re to build a small to medium web app.

Will you go full Laravel + blade (Coupled)?

OR

Do you prefer decoupling the backend and frontend…and use JS Frameworks (Decoupled)?

https://redd.it/1qdvr1t
@r_php
Multiplayer Game of Life

https://gameoflife.zweiundeins.gmbh

This demonstrates a Swoole app streaming 2500 divs 5 times a second to the browser via SSE. As SSE is just HTTP, it's Brotli-compressed and manages 100x compression after a few minutes, due to Brotli window spanning the entire stream. It's multiplayer, so open two tabs side by side to see. A year ago I never thought somesthing like this possible with PHP - this runs on a 20$/year VPS.

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