Giter Site home page Giter Site logo

Request: any and all routes about framework HOT 5 CLOSED

laravel avatar laravel commented on May 5, 2024
Request: any and all routes

from framework.

Comments (5)

DPr00f avatar DPr00f commented on May 5, 2024 45

That's it, ty very much. +1

Route::any('{all}', function(){
    return 'It Works';
})->where('all', '.*');

from framework.

bencorlett avatar bencorlett commented on May 5, 2024 1

@DPr00f

Firstly, a 404 in L4 is handled by catching a Symfony\Component\HttpKernel\Exception\HttpNotFoundException so that's how you handle your 404's.

How you do the same in L4 is by appending where() after the declaration of a route, where the first parameter is the named param in the route and the second param is a regular expression which must match true. If it does not, the route is not executed and another match is found (if it exists). For example (don't quote me on syntax errors :P):

<?php

// GET /foo/123/some/other/value/here
// GET /foo/a123/sdf will not work as "a123" is not numeric.
Route::get('foo/{bar}/{baz}', function($bar, $baz)
{
    // $bar == 123
    // $baz == 'some/other/value/here'

})->where('bar', '\d+') // Digits
  ->where('baz', '.*');  // Anything

// You can modify your REGEX to suit:

// GET /foo/a123/asdf
// GET /foo/z123/asdf
// GEt /foo/df123/asdkfjsd
Route::get('foo/{bar}/{baz}', function($bar, $baz)
{

})->where('bar', '[a-z]{1,2}\d+') // 1-2 Letters + Digits
  ->where('baz', '.*');  // Anything

Have a look at http://symfony.com/doc/2.0/components/routing/introduction.html for more as L4 uses the Symfony routing as it's backbone.

Also, check out http://gskinner.com/RegExr/ for a really intuitive way to build up regular expressions :)

from framework.

bencorlett avatar bencorlett commented on May 5, 2024

No problem, glad I could help.

I was a bit disappointed to see (:all) go at first until I saw the potential of this solution!

I'd recommend closing the issue now :)

from framework.

niallobrien avatar niallobrien commented on May 5, 2024

This is covered in the Laravel docs at four.laravel.com is it not? Also, @jasonlewis has a great blog post on this issue.

from framework.

francoishill avatar francoishill commented on May 5, 2024

None of the above works in Laravel 4.0. Here is the solution:

Route::any('{firstPart}/{rest}', function($firstPart, $rest){
    return Response::make($firstPart . ", " . $rest);
})->where('firstPart', '[^/]*')->where('rest', '.*');

from framework.

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.