Giter Club home page Giter Club logo

Comments (16)

nunomaduro avatar nunomaduro commented on May 15, 2024 9

Currently developing a solution that allows you to define the name of the route on the view itself:

use function Laravel\Folio\{name};

name('users-list');

?>

<div>
    // ...
</div>

from folio.

taylorotwell avatar taylorotwell commented on May 15, 2024 1

Probably would need an array argument of names we can pass to Folio::route that correspond to pages. Defining the name within the page itself would be convenient but we would have to scan every possible page if we don't have a cache building step.

from folio.

inmanturbo avatar inmanturbo commented on May 15, 2024

It looks like Folio sets the route names based on this pattern: 'folio-'.substr(sha1([baseUri]), 0, 10);

<?php

namespace Laravel\Folio;

class MountPath
{
    /**
     * The path based middleware for the mounted path.
     */
    public PathBasedMiddlewareList $middleware;

    /**
     * Create a new mounted path instance.
     */
    public function __construct(public string $path,
        public string $baseUri,
        array $middleware = [])
    {
        $this->middleware = new PathBasedMiddlewareList($middleware);
    }

    /**
     * Get the route name assigned to the route at this mount path.
     */
    public function routeName(): string
    {
        return 'folio-'.substr(sha1($this->baseUri), 0, 10);
    }
}
   // Laravel\Folio\FolioManager;
    /**
     * Register a route to handle page based routing at the given paths.
     *
     * @throws \InvalidArgumentException
     */
    public function route(string $path = null, ?string $uri = '/', array $middleware = []): static
    {
        $path = $path ? realpath($path) : config('view.paths')[0].'/pages';

        if (! is_dir($path)) {
            throw new InvalidArgumentException("The given path [{$path}] is not a directory.");
        }

        $this->mountPaths[] = $mountPath = new MountPath($path, $uri, $middleware);

        if ($uri === '/') {
            Route::fallback($this->handler($mountPath))
                ->name($mountPath->routeName());
        } else {
            Route::get(
                '/'.trim($uri, '/').'/{uri?}',
                $this->handler($mountPath)
            )->name($mountPath->routeName())->where('uri', '.*');
        }

        return $this;
    }

from folio.

mokhosh avatar mokhosh commented on May 15, 2024

It looks like Folio sets the route names based on this pattern: 'folio-'.substr(sha1([baseUri]), 0, 10);

Doesn't look like it's naming "routes". It's just the name of one fallback route that Folio is using to process all the urls that you haven't captured with your routes.

Named routes is the first thing I stumbled upon trying to migrate a project I had to Folio.

from folio.

inmanturbo avatar inmanturbo commented on May 15, 2024

should be able to use it with something like route('folio-'.substr(sha1('pages'), 0, 10), ['uri' => 'users'] ) if I'm not mistaken, if for instance you set the base uri to 'pages' in the Folio service provider:

        Folio::route(resource_path('views/pages'), uri: 'pages' , middleware: [
            '*' => [
                //
            ],
        ]);

Would be nice to have a helper or something

function folioRouteName(string $uri): string
{
     return 'folio-'.substr(sha1($uri), 0, 10);
 }

would whittle that down to route(folioRouteName('pages'), ['uri' => 'users'] ).

from folio.

inmanturbo avatar inmanturbo commented on May 15, 2024

Even better I guess would be to have a pattern or convention for naming routes based on the segments or paths, and/or maybe be able import a function in the blade file to name the route. I don't believe the package traverses the directory structure at all until it's hit with a request though, so as it stands now they couldn't be indexed or named ahead of time.

from folio.

mokhosh avatar mokhosh commented on May 15, 2024

How about a nice convention, since the whole point of Folio is routing by convention and folder structure?
It would easily work since I guess you wouldn't use Folio for updating or deleting or other "resource" operations.

Something like a dot notation name based on the folder structure:

users/[User:uuid] -> 'users.user'
posts -> 'posts'

You could add an optional prefix in some settings somewhere to avoid possible collisions with web.php routes. Something like folio.*

from folio.

inmanturbo avatar inmanturbo commented on May 15, 2024

How about a nice convention, since the whole point of Folio is routing by convention and folder structure? It would easily work since I guess you wouldn't use Folio for updating or deleting or other "resource" operations.

Something like a dot notation name based on the folder structure:

users/[User:uuid] -> 'users.user'
posts -> 'posts'

You could add an optional prefix in some settings somewhere to avoid possible collisions with web.php routes. Something like folio.*

This would be a nice default, but it doesn't allow much flexibility or control, also it would require that folio view path be scanned recursively when the application boots

from folio.

mokhosh avatar mokhosh commented on May 15, 2024

I think the spirit of Folio is not flexibility or configuration.
I think it's a tool that you reach out when you want to throw in your views and expect everything to just work.

If you want control and flexibility why not just use the good routes?

from folio.

inmanturbo avatar inmanturbo commented on May 15, 2024

True, but it being extensible doesn't have to mean it won't "just work" as is.

It would be convenient to "just know" your route names based the directory structure or file path, The only challenge really is it would require some kind or caching step or something: the directory would have to be scanned to load the route names. Unless it was implemented manually using an array. However, it would be really neat for it to "just work" the way you've illustrated above.

And by the way, if you are defining all your routes and names manually anyway using an array, why not just use traditional routes?

from folio.

tpetry avatar tpetry commented on May 15, 2024

Probably would need an array argument of names we can pass to Folio::route that correspond to pages. Defining the name within the page itself would be convenient but we would have to scan every possible page if we don't have a cache building step.

Its already recommended to run route:cache when deploying. Can't we make a syntax like this and add a warning that route caching is needed for good performance?

<?php

use function Laravel\Folio\{withNamedRoute};

withNamedRoute('user.display');

?>

<div>
    User {{ $user->id }}
</div> 

from folio.

ryangjchandler avatar ryangjchandler commented on May 15, 2024

@tpetry Folio pages aren't registered as "real" routes, there are only a handful of routes that use wildcard route parameters to match again pages. I'm working on #54 which is a first step in getting some support in places, but it's much more likely that a folio:cache command or something will be introduced to add support for a name() function or similar.

from folio.

tpetry avatar tpetry commented on May 15, 2024

@ryangjchandler That's why I proposed manually registering routes - exactly like you can assign HTTP kernel middlewares. And still keep the prominent feature of Folio not having a central place you have to register routes. Because otherwise we could also add routes in the web.php with something like Route::folio() as you are working on. But you're right, your work could be the building block for more.

from folio.

inmanturbo avatar inmanturbo commented on May 15, 2024

There may be a good way to handle this outside the scope of folio. For instance What if there was a way to map route names to route paths, without assigning a handler? Something like the following api:

// Somewhere in a service provider (or bootstrap/app.php in L11):

Route::define(name: (string) 'users.index', path: (string) '/users', params: null); //  ...params

if there is not handler set for the path such a route would simply return 404 (or even an exception reminding to handle the route) , and it could be accessed using the route() helper. Under php artisan routes:list the path could be shown pointing to the defined name and the folio route, or fallback handler, if one exists, or simply errors::404 if one does not.

from folio.

mokhosh avatar mokhosh commented on May 15, 2024

@nunomaduro can you please comment on the convention idea as well?

from folio.

nunomaduro avatar nunomaduro commented on May 15, 2024

Lets follow this issue on the pull request now: #79.

from folio.

Related Issues (20)

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.