Code Monkey home page Code Monkey logo

Comments (9)

knusperpixel avatar knusperpixel commented on May 8, 2024

The docs (http://flightphp.com/learn#routing) say that "You can pass execution on to the next matching route by returning true from your callback function".

So I would think, something like this should work:

// Setup possible languages
Flight::set('possible_languages', array('en', 'de', 'fr', 'it'));

// Set default language if not present in url
Flight::set('language', 'en');

// Setup route to check the language
Flight::route('(/@lang:[a-z]{2})', function($language){

    // Check if valid language
    if (in_array($language, Flight::get('possible_languages'))) {

        // Set language
        Flight::set('language', $language);

        // Continue to next route
        return true;
    }
});

// define all other routes

from core.

kamov avatar kamov commented on May 8, 2024

Fabulous! I will try.

Thank you.

from core.

kamov avatar kamov commented on May 8, 2024

Hi,

I try use your code, and the problem is that next route is not found.

I change a bit the code and now seem work.

Check below:

Flight::set('possible_languages', array('en', 'de', 'fr', 'it'));// Setup possible languages
Flight::set('language', 'en');// Set default language if not present in url
// Setup route to check the language - add here /[a-z]+ because is not found the next route
Flight::route('(/@lang:[a-z]{2}/[a-z]+)', function($lang){ 
    // Check if valid language
    if (in_array($lang, Flight::get('possible_languages'))) {
        // Set language
        Flight::set('language', $lang);
        // Continue to next route
        return true;
    }
});
/* Others Routing */
Flight::route('/[a-z]{2}/login', array('AuthController','login')); // added [a-z]{2}

URL: www.example.com/it/login => work fine, firstly is load first route, then second route

What do you think?

Bye

from core.

sah-comp avatar sah-comp commented on May 8, 2024

Following the suggested code i have found that i have to add a piece of (/[0-9a-z]+) in the default language route for every segment of my URLs i use later on.

Shouldn't it be possible to write:

Flight::route('(/@language:[a-z]{2})(/*)', function($language) {});

instead of

Flight::route('(/@language:[a-z]{2})(/[0-9a-z]+)(/[0-9a-z]+)', function($language) {});

Here is my routes.php so far. It works as expected, but if a want to add a route for e.g. /editor/@type/@action(@id) i will have to add (/[0-9a-z]+) to my language setter route as many times as my urls may get deep?!

/**
 * Change the default language.
 */
Flight::route('(/@language:[a-z]{2})(/[0-9a-z]+)(/[0-9a-z]+)', function($language) {
    error_log('set lang...' . $language);
    if (in_array($language, Flight::get('possible_languages'))) {
        Flight::set('language', $language);   
    }
    return true;
});

/**
 * Route the root to our welcome controller.
 */
Flight::route('(/[a-z]{2})/', array('Controller_Welcome', 'index'));

/**
 * Route to the system controller.
 */
Flight::route('(/[a-z]{2})/admin', array('Controller_Admin', 'index'));
Flight::route('(/[a-z]{2})/admin/index', array('Controller_Admin', 'index'));

/**
 * Show a 404 error page if no route has jumped in yet.
 */
Flight::map('notFound', function() {
    Flight::render('404', array(), 'content');
    Flight::render('html5', array(
        'language' => Flight::get('language'),
        'title' => I18n::__('notfound_head_title')
    ));
});

from core.

kamov avatar kamov commented on May 8, 2024

I have the same problem, I also need add /[a-z]{2}/ to any routes...

Not find yet a solution.

I hope @mikecao can help

Otherwise with .htaccess can be fixed in some way.

from core.

mikecao avatar mikecao commented on May 8, 2024

Unfortunately you will need to add the language detection /[a-z]{2}/ to all your routes because it exists as part of the url. What you can do is use name parameters so you don't have to create so many custom routes:

Flight::route('(/@lang:[a-z]{2})/@page', function($lang, $page){
    if (!empty($lang)) {
        // Set language
    }

    // Load page controller
});

@sah-comp There was a bug in the router code. With the latest commit f647748 you should now be able to do:

Flight::route('/@lang:[a-z]{2}/*', function($lang){
    // Set language
});

from core.

sah-comp avatar sah-comp commented on May 8, 2024

For me this is the fix.

I can now use the following as my first route:

Flight::route('(/@language:[a-z]{2})(/*)', function($language) {
     // set the language...
     return true;
});

//other routes with optional language code follow...

Thanks so much.

from core.

fatihmert avatar fatihmert commented on May 8, 2024

Hello,

I guess, I'm late :/ but I don't want create new commit.
My lang route func that like; (but doesn't work, flifhtphp show to 404 error)

Flight::route('(/@lang:[a-z]{2})',function($lang){
    foreach (glob("lang/*.php") as $dosya) {
        if(basename($dosya,".php") == $lang){
            Flight::set('lang',$lang);
            return true;
        }
    }
});

include("lang/".Flight::get('lang').".php");

Flight::route('/',function(){
    echo "Hello";
});

from core.

mikecao avatar mikecao commented on May 8, 2024

@fatihmert Your use of include it will run immediately where that line is, which is before Flight even starts up.

from core.

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.