Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dynamic file extensions #51

Closed
wants to merge 5 commits into from
Closed

Add support for dynamic file extensions #51

wants to merge 5 commits into from

Conversation

ZebTheWizard
Copy link

@ZebTheWizard ZebTheWizard commented Jul 27, 2023

Makes it possible to support non .blade.php extensions. Intended for use with Inertia.

Syntax:

Folio::extension('.vue')

Example Inertia renderer for Folio:

use Illuminate\Http\Request;
use Laravel\Folio\Folio;
use Laravel\Folio\Pipeline\MatchedView;
use Inertia\Inertia;

//...

Folio::extension('.vue');

Folio::renderUsing(function (Request $request, MatchedView $matchedView) {
    return Inertia::render($matchedView->componentPath(), $matchedView->data);
});

Folio::route(resource_path('js/Pages'), middleware: [
    '*' => [
        //
    ],
]);

@inmanturbo
Copy link
Contributor

inmanturbo commented Jul 27, 2023

It would be nice if more than one extension could be used at a time in a single application. For instance if you could pass the extension in the route method, and render with a markdown parser in one directory, then call Folio::route() again to use blade or vue from another.

Or otherwise have an array of supported extensions that one can check for when calling rederUsing(), which would require a bit more work in the Router pipeline.

@ZebTheWizard
Copy link
Author

It would be nice if more than one extension could be used at a time in a single application. For instance if you could pass the the extension in the route method, and render with a markdown parser in one directory, then call Folio::route() again to use blade or vue from another.

Or otherwise have an array of supported extensions that one can check for when calling rederUsing(), which would require a bit more work in the Router pipeline.

This might be something for another PR. Otherwise, I gave control to MountPath in case there is a future update for multiple routers. Latest commit works with method chaining:

Folio::withExtension('.svelte')->renderUsing(function (Request $request, MatchedView $matchedView) {
    return Inertia::render($matchedView->componentPath(), $matchedView->data);
})->route(resource_path('js/Pages'), middleware: [
    '*' => [
        //
    ],
]);

@taylorotwell
Copy link
Member

taylorotwell commented Jul 27, 2023

I think this is kinda neat, but, unlike Volt / PHP there would be no convenient way to get additional data into the Inertia page. For example if you needed to load in additional data via a separate query / API call to pass to the Inertia page.

@inmanturbo
Copy link
Contributor

inmanturbo commented Jul 27, 2023

I think this is kinda neat, but, unlike Volt / PHP there would be no convenient way to get additional data into the Inertia page. For example if you needed to load in additional data via a separate query / API call to pass to the Inertia page.

Can the routes be intercepted/overridden by a traditional route or controller? For instance if it was insured that folio routes boot before manual routes, then allow portions of the route segments be overridden or intercepted in the event that it needs a little more than what folio can provide.

This would allow you to quickly build out all your basic stuff, then go back through and sprinkle in a route/controller here and there where needed.

EDIT:

Crossed out a section. I was thinking about it backwards, being it's a fallback route.

@ZebTheWizard ZebTheWizard changed the title Add support for dynamic file extensions [Draft] Add support for dynamic file extensions Jul 27, 2023
@ZebTheWizard
Copy link
Author

ZebTheWizard commented Jul 27, 2023

Can the routes be intercepted/overridden by a traditional route or controller?

Yes, in a fresh Laravel app, the routing defined using the RouteServiceProvider will take precedence over the routing defined in the FolioServiceProvider. Complex queries should take place in middleware or in a dedicated route/controller.

// web.php

Route::get('/stories/breaking-news', function () {
    $news = News::with(['images', 'comments'])
         ->whereBreaking()
         ->orderBy('edited_at', 'desc')
         ->get();
    
    Inertia::render('/news/index', compact('news'));
});

The coexisting folder structure lets you fetch data from route parameters such as a news story with a unique slug:

/
└── resources
    └── js
        └── Pages
            └── index.svelte
                └── stories
                    ├── [Story].svelte
                    └── index.svelte

@ZebTheWizard ZebTheWizard changed the title [Draft] Add support for dynamic file extensions Add support for dynamic file extensions Jul 27, 2023
@taylorotwell
Copy link
Member

I may revisit this at some point but going to let Folio settle a bit first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants