Comments (9)
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.
Fabulous! I will try.
Thank you.
from core.
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.
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.
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.
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.
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.
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.
@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)
- Compatibility issue with PHP 8.1-8.2 HOT 7
- Route preg_match() error HOT 1
- display_errors does not work with flight HOT 4
- route view rendered after redirect HOT 7
- Argument #1 ($ex) must be of type Exception, TypeError given HOT 2
- Cors in Framework HOT 5
- describing routes HOT 3
- Slashes In Routes HOT 2
- Website outdated version HOT 1
- I make a new framework βAlightβ, can anyone try it and give me some feedback? HOT 1
- Flushing buffer results in Response->write() error HOT 1
- Migrate to Flight 2 HOT 1
- Future of the Flight Micro Framework HOT 19
- Naming routes - help for navigation HOT 4
- Example projects - What would that look like ? HOT 14
- Possible integration with Roadrunner and Swoole. HOT 2
- Possible way to generate OpenAPI specification on the fly. HOT 1
- Possible deployment on AWS Lambda. HOT 1
- Possible way to include some shorthand helpers or utils. HOT 12
- Documentation on how to Docker-ize FlightPHP application. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.