DEV Community

marius-ciclistu
marius-ciclistu

Posted on • Originally published at marius-ciclistu.Medium on

Why I Migrated from Laravel to Maravelith and Then Finally to Maravel


Maravel vs Maravelith

I migrated some projects from Lumen to Maravel and some from Laravel to Maravelith to keep the compatibility because at that time , Maravel did not have out-of-the-box support for domain routing, named routes by domain, session, cookies, CSRF protection and cached views.

But after I added those missing pieces in Maravel , the difference in boot speed between Maravel and Maravelith made me decide to migrate.

There are some things that I lost, like fancy error pages in web routes, but I don’t care about those. This simple replacement does the job just fine in Maravel:

/**
     * Prepare a response for the given exception.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Throwable $e
     * @return \Illuminate\Http\Response
     */
    protected function prepareResponse($request, Throwable $e)
    {
// $response = new Response(
        $response = \di(Response::class, [
            \config('app.debug', false) ? $this->renderExceptionWithSymfony($e, true) : '<html>' .
                '<body style="display: grid; place-content: center; height: 100vh; margin: 0; text-align: center;">' .
                '<h1>' . (
                    $status = $this->isHttpException($e) ?
                        $e->getStatusCode() :
                        \min($e->getCode() ? $e->getCode() : 500, 500)
                ) . ': ' . __(Response::$statusTexts[$status] ?? [
                    419 => 'Page Expired'
                ][$status] ?? 'Server Error') . '</h1></body></html>',
            $this->isHttpException($e) ? $e->getStatusCode() : 500,
            $this->isHttpException($e) ? $e->getHeaders() : [],
        ]);

        $response->exception = $e;

        return $response;
    }
Enter fullscreen mode Exit fullscreen mode

The improved deferred service providers system from Maravel is far superior to that of Maravelith.

You pay (in boot time) JUST for what/if/when YOU use!

Places that need attention when migrating from Maravelith/Laravel to Maravel:

  • The authentication logic , including DB tables and tokens. If you use Sanctum, you must migrate your access tokens from the personal_access_tokens table to the new auth logic.
  • Route registration.
  • JobClassName::dispatch(…)->delay(…); must be transformed into \dispatch(\app(JobClassName::class, […])->delay(…));.
  • \resolve calls must be changed to \app or \di.
  • All Illuminate\Foundation prefixed FQNs must be replaced with their Maravel/Lumen counterparts.
  • ->setRedirector($app->make(Redirector::class)); must be deleted if you use it in your code.
  • app/Http/Middleware/Authenticate.php must be changed.
  • Add this to routes/web.php:
include 'api.php';

// use these middleware on all web routes
$router->group([
    'domain' => trim(\config('app.url')),
    'middleware' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
    ]
], function (\App\Router $router): void {
    // ...
});
Enter fullscreen mode Exit fullscreen mode
  • And add \App\Http\Middleware\VerifyCsrfToken::class ONLY on the routes that expect a __token generated via csrf_token().
  • Add these to \App\Application::registerExplicitBindingsMap:
/**
 * To avoid calls to
 * @see \Laravel\Lumen\Concerns\RoutesRequests::middleware()
 * Make sure this list has UNIQUE values!!!
 */
$this->middleware = [
    \App\Http\Middleware\TrustProxies::class,
    //\Illuminate\Http\Middleware\HandleCors::class,
    \App\Http\Middleware\TrimStrings::class,
];

/**
 * To avoid calls to
 * @see \Laravel\Lumen\Concerns\RoutesRequests::routeMiddleware()
 * Note that you can use the middleware FQN on a route without declaring its alias here!
 */
$this->routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
];
Enter fullscreen mode Exit fullscreen mode
  • Lastly, you should use a tool like Meld to compare and merge the Maravel template folder structure and file contents with your project. Files like artisan, bootstrap/app.php, config files, and others are not the same.

Top comments (0)