I want to follow the following two patterns of routes.

user/repo/branch/path1/path2 (no matter how many paths are in place)

If only the bottom two are
Route :: get ('/{user}/{repo}/{branch}/{path}','SearchController @ show')->where ('path','. *&Apos;) ;
It will be routed like this, but if it is at the top (no path part), an error will occur.


If i have @@@. @@@ in the end path, I want to route.
I want to make sure that there are as many paths as possible.

Error message
I don't know how to route above.

Route :: get ('/{user}/{repo}/{branch}/{path?}&Apos;,'SearchController @ show')->where ('path?&Apos;,'. *');
I tried to specify a syntax error.

If i only want to pass ①
Route :: get ('/{user}/{repo}/{branch}/{path}','SearchController @ show')->where ('path','. *&Apos;) ;
Route :: get ('/{user}/{repo}/{branch}/{path?}&Apos;,'SearchController @ show');
Then, it will be able to pass through whatever path is below the branch (even in the case of user/repo/branch), but
When @@@@. @@@ path comes, the show action will move.
In the case of @@@@. @@@ I want to move a different action.

Route :: get ('/{user}/{repo}/{branch}/{path}','SearchController @ show')->where ('path','*. *&Apos;);
Route :: get ('/{user}/{repo}/{branch}/{path}. {Ext}','SearchController @ show')->where ('path','* . *');

I tried both, but neither worked.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

  • Answer # 1

    If you remember that the route where condition ischecked with regular expressions, you can do what you want by devising it!


      user/repo/branch/path1/path2 (no matter how many paths are in place)

    I think that this is enough for route specification.

    Route :: get ('/ {user}/{repo}/{branch}/{path?}', 'SearchController @ show')->where ('path', '. *' );

    The?appended to the end of the route parameter name is a sign that it may not exist, so it is not necessary to include it in the route parameter name of the where condition .


      If @@@. @@@ is in the last path, I want to move a different action.

    * We will proceed with assuming that there is more than one path before @@@. @@@.

    If the extension part (the part after the .dot) is fixed (or has a pattern), I think that it is better to make it a condition, but "@@@. @@@" Does it mean that the part will also change?

    I think the route is written like this.
    * Specify the action you want to move with @@@. @@@ for the ●●● part

    Route :: get ('/ {user}/{repo}/{branch}/{path}/{file}. {ext}', 'SearchController @ ●●●')->where ('path', '. * [^ /] {1} $')->where ('file', '[^. /] +')->where ('ext', '[^./] + ');
    Route :: get ('/ {user}/{repo}/{branch}/{path?}', 'SearchController @ show')->where ('path', '. *');

    For ●●● action (@@@. @@@ route),
    The path condition does not end with/,
    The condition of file does not include. (dot) and/
    The ext condition is. (dot) andnot including /.

    The show route is written after the action route setting of ●●●.
    Now, "/ {user}/{repo}/{branch}/~~~~" that does not match the action route of ●●● will be picked up at the show.

    With this specification,
    ●●● Action includes, for example,
    When accessing with user/repo/branch/path1/path2/aaaa.html

    {path} is "path1/path2"
    {file} contains "aaaa"
    {ext} must be "html"

    Will be passed.

    ★ Notes on the above route specification ★
    As I wrote earlier, isn't it strange? Even URLs that you think
    If it ’s in the form of "/ {user}/{repo}/{branch}/~~~~"
    It will be picked up by the route of the show, so if there is something to be excluded, it will be necessary to change the where condition (regular expression) of the "path" of the show.

    Is it strange? Here is an example of a URL that I think ...

    user/repo/branch/aaa.html ← If there is no path part
    user/repo/branch/path1/aaa.html/path2<-URL order is wrong
    user/repo/branch/path1/path2/.html ← user/repo/branch/path1/path2/aaaa..html ← There are many dots

    * Because we have only verified the operation, there may be omissions or mistakes in the regular expression. . .
    In that case, think about the URL that should be correct, and build a regular expression to match it.

  • Answer # 2

    This was answered last time at https://StackOverflow.com/questions/142664.

    If you don't have a path, route

    Route :: get ('/ {user}/{repo}/{args}', 'SearchController @ show')->where ('args',' ^ (?!. * \. html $). * $');
    Route :: get ('/ {user}/{repo}/{args}', 'SearchController @ html')->where ('args', '. *');

    etc. and on the controller side

    public function show ($user, $repo, $args)
        $paths = explode ('/', $args);
        $branch = array_shift ($paths);
        // ...
    public function html ($user, $repo, $args)
        $paths = explode ('/', $args);
        $branch = array_shift ($paths);
        // ...
    If you give it as

    ,$branchcan be acquired well, and the path part is stored as an array in$paths.

    And when it ends with.html, it will execute thehtmlmethod.