Coder Social home page Coder Social logo

laravel's Introduction

Laravel for Platform.sh

Deploy on Platform.sh

This template provides a basic Laravel skeleton. It comes pre-configured to use a MariaDB database and Redis for caching and sessions using a Laravel-specific bridge library that runs during Composer autoload. The public files symlink is also replaced with a custom web path definition so it is unnecessary. It is intended for you to use as a starting point and modify for your own needs.

Laravel is an opinionated, integrated rapid-application-development framework for PHP.

Features

  • PHP 8.0
  • MariaDB 10.4
  • Redis 5.0
  • Automatic TLS certificates
  • Composer-based build

Notice

On Platform.sh the minimum time between cron jobs being triggered depends on your plan. Laravel documentation suggests running the scheduler as a cron job every minute. Task scheduling may then be contradicted by the cron minimum frequency. Schedules outside the specified cron frequency are ignored and the related tasks aren’t triggered.

Due to this conflict, this Laravel template utilizes workers to run both the scheduler and the queue systems. In order to have enough resources to support these workers as well as the MariaDB and Redis service, this template requires at least a Medium plan.

Customizations

The following changes have been made relative to a plain Laravel project. If using this project as a reference for your own existing project, replicate the changes below to your project.

  • The .platform.app.yaml, .platform/services.yaml, and .platform/routes.yaml files have been added. These provide Platform.sh-specific configuration and are present in all projects on Platform.sh. You may customize them as you see fit.
  • An additional Composer library, platformsh/laravel-bridge, has been added. It automatically maps Platform.sh's environment variables to Laravel's environment variables where possible. It leverages the platformsh/config-reader library.
  • The Laravel Bridge library also automatically configures Laravel to use Redis for both caching and session storage. That may be disabled by removing or changing the name of the rediscache and redissession relationships in .platform.app.yaml.
  • Laravel normally wants you to create a symlink for the public storage directory, using the artisan storage:link command. That is not necessary and will not work on Platform.sh due to the read-only file system. Instead, a dedicated web path mapping is included for the /storage path that has the same effect.

References

laravel's People

Contributors

actions-user avatar chadwcarlson avatar crell avatar gilzow avatar moisesbenzan avatar ncuxap avatar otaviojava avatar platformsh-devrel avatar thomasdiluccio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

laravel's Issues

RedirectIfAuthenticated middleware API support

What in this template can be improved or added as a feature?

Hi guys, i think that the built-in RedirectIfAuthenticated middleware (https://github.com/platformsh-templates/laravel/blob/master/app/Http/Middleware/RedirectIfAuthenticated.php) not work correctly in the context of a Laravel Breeze application scaffolded with api option (php artisan breeze:install api) or in general in the context of a client-driven authentication where Laravel act as a pure Backend.
Practically the middleware performs an authentication check and if it is already authenticated it is redirected to the HOME (/dashboard).

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @param  string|null  ...$guards
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next, ...$guards)
    {
        $guards = empty($guards) ? [null] : $guards;

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                return redirect(RouteServiceProvider::HOME);
            }
        }

        return $next($request);
    }
}

If we take as an example a SPA where routing is managed on the client side this approach does not work. If the client call an API middlewared by RedirectIfAuthenticated the client will receive a bad response because it expects a JSON and not a redirect.

In my opinion the communication mode in this middleware should therefore be considered.

What exactly should be updated?

It would therefore be sufficient to add a check on the response expectation of the request with $request->expectsJson() and if the expectation is to receive a JSON then return an error message "Already authenticated." with a HTTP status code 200, the default return, the default would remain a redirect.

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @param  string|null  ...$guards
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next, ...$guards)
    {
        $guards = empty($guards) ? [null] : $guards;

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                if ($request->expectsJson()) {
                    return response()->json(['error' => 'Already authenticated.']);
                }
                return redirect(RouteServiceProvider::HOME);
            }
        }

        return $next($request);
    }
}

How important is this feature to you?

I think this problem is not blocking for me and for most users of this middleware, but I encountered this request on StackOverflow related to this -> https://stackoverflow.com/questions/74988485/fresh-laravel-sanctum-api-redirecting-to- dashboard-route which I answered and which appears to have been viewed several times.

Additional context

No response

Laravel 10 cron

What in this template can be improved or added as a feature?

Laravel 10 scheduling requires cron to run every minute, this is currently not possible with platform so a work around needs to be documented.

What exactly should be updated?

Cron schedule

How important is this feature to you?

Scheduling is not useable in laravel 10

Additional context

No response

Crons are not being run due to the queue:work blocking it

Intro

crons:
    # Run Laravel's scheduler every 5 minutes, which is often as crons can run.
    scheduler:
        spec: '*/5 * * * *'
        cmd: 'echo "testing"; php artisan schedule:run'

app/Console/Kernel.php has a simple:

$schedule->call(function () {
            echo "This is a test every 5 minutes";
        })->everyFiveMinutes();

Expected result

Log result when you happen to deploy it on minute 00 05 10 15 20 25 30 35 40 45 50 55

testing
[2021-08-30 13:30:38.741147] Launching command 'php artisan schedule:run'.
Running scheduled command: Callback
This is a test every 5 minute

Actual failed result
However, if you deploy it on any other minute: (or we do a maintenance and move it... )


[2021-08-30 13:36:33.361473] Launching command 'echo "testing"; php artisan schedule:run'.

testing
No scheduled commands are ready to run.

Notice the distinct lack of running the actual scheduled command. This is because Laravel currently assumes that we run a cron every minute, and it therefore assumes that it will encounter 00 05 10 15 ... at some point. (which it doesn't)

Proposal to solve

Make a service provider that overrides the CronExpression and injects our own, that can deal with the shift of minutes.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class PlatformShServiceProvider extends ServiceProvider
{
    /**
     * All of the container bindings that should be registered.
     *
     * @var array
     */
    public $bindings = [
        CronExpression::class => PlatformShCronExpression::class,
    ];

TokenMismatchException

Hello, i have a errror when build in laravel
Code : 500
Error : TokenMismatchException in VerifyCsrfToken.php line 68

I checked my Request headers and sure have x-csrf-token: JWvtbnS93EeaY63F6TaRH5KPZrsSe0sEjv1QYPus

My .platform.app.yml mount

The 'mounts' describe writable, persistent filesystem mounts in the application.

mounts:
"storage/app/public":
source: local
source_path: "public"
"storage/framework/views":
source: local
source_path: "views"
"storage/framework/sessions":
source: local
source_path: "sessions"
"storage/framework/cache":
source: local
source_path: "cache"
"storage/logs":
source: local
source_path: "logs"
"bootstrap/cache":
source: local
source_path: "cache"
"/.config":
source: local
source_path: "config"
Screenshot_5
Screenshot_6

Thanks.

Template fails on Redis install

I noticed 2 issues which may be related.

  • Installing the Laravel template using the platform.sh wizard fails - an empty repo is created
  • When I manually copy the example from this repo and attempt to push it to platform.sh, I get a failure on redis install:
Total 198 (delta 35), reused 0 (delta 0), pack-reused 0

Validating submodules

Validating configuration files

E: Error parsing configuration files:
    - applications: The "redis" extension is not supported for php:7.4
        <unknown>
To git.us-2.platform.sh:h4fgy55i6fzrq.git
 ! [remote rejected] master -> master (invalid configuration files)
error: failed to push some refs to '[email protected]:h4fgy55i6fzrq.git'

Mount warnings

Describe the bug

When I create a project from this template every time I commit new changes and push I get the the following warnings

Include some logs

  W: The mount '/app/storage/framework/views' has a path that overlaps with a non-empty folder.
  The content of the non-empty folder either comes from:
   - your git repository (you may have accidentally committed files).
   - or from the build hook.
  Please be aware that this content will not be accessible at runtime.
  
  
  W: The mount '/app/storage/framework/cache' has a path that overlaps with a non-empty folder.
  The content of the non-empty folder either comes from:
   - your git repository (you may have accidentally committed files).
   - or from the build hook.
  Please be aware that this content will not be accessible at runtime.
  
  
  W: The mount '/app/storage/logs' has a path that overlaps with a non-empty folder.
  The content of the non-empty folder either comes from:
   - your git repository (you may have accidentally committed files).
   - or from the build hook.
  Please be aware that this content will not be accessible at runtime.
  
  
  W: The mount '/app/storage/app/public' has a path that overlaps with a non-empty folder.
  The content of the non-empty folder either comes from:
   - your git repository (you may have accidentally committed files).
   - or from the build hook.
  Please be aware that this content will not be accessible at runtime.
  
  
  W: The mount '/app/bootstrap/cache' has a path that overlaps with a non-empty folder.
  The content of the non-empty folder either comes from:
   - your git repository (you may have accidentally committed files).
   - or from the build hook.
  Please be aware that this content will not be accessible at runtime.
  
  
  W: The mount '/app/storage/framework/sessions' has a path that overlaps with a non-empty folder.
  The content of the non-empty folder either comes from:
   - your git repository (you may have accidentally committed files).
   - or from the build hook.
  Please be aware that this content will not be accessible at runtime.

Reproducing

Create a project from this template.

Expected behavior

No warnrings?

Your environment

Development

Screenshots

No response

Additional context

No response

[Enhancement] Add support for auto-updating

What in this template can be improved or added as a feature?

Template should self auto-update via source operations and workflows

What exactly should be updated?

necessary github workflow files

How important is this feature to you?

N/A

Additional context

No response

Project production environment returning 500 error

Describe the bug

During VRT tests for PR #46 the production environment returned a 500 error. Redeployed production and it reported success. Only issue I noticed in the deploy is

  Executing deploy hook for application app
    
    In Application.php line 749:
                                                                         
      Class "BeyondCode\DumpServer\DumpServerServiceProvider" not found  

Error log shows

2023/02/13 22:12:14 [error] 224#0: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught ReflectionException: Class "cache" does not exist in /app/vendor/laravel/framework/src/Illuminate/Container/Container.php:875

Include some logs

2023/02/13 22:12:14 [error] 224#0: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught ReflectionException: Class "cache" does not exist in /app/vendor/laravel/framework/src/Illuminate/Container/Container.php:875

Reproducing

N/A

Expected behavior

Server response of 200

Your environment

Platform.sh

Screenshots

No response

Additional context

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.