c9s / pux Goto Github PK
View Code? Open in Web Editor NEWPux is a fast PHP Router and includes out-of-box controller tools
License: MIT License
Pux is a fast PHP Router and includes out-of-box controller tools
License: MIT License
Sample code:
<?php
require __DIR__ . '/../vendor/autoload.php';
$mux = new Pux\Mux;
$mux->add('/', [ 'PageController', 'page1' ]);
$route = $mux->dispatch('/abc');
var_dump($route);
Result of 1.4.0:
NULL
Result of 1.5.0:
array(4) {
[0] =>
bool(false)
[1] =>
string(1) "/"
[2] =>
array(2) {
[0] =>
string(14) "PageController"
[1] =>
string(5) "page1"
}
[3] =>
array(0) {
}
}
Is this a bug or not?
Is there support for PATCH and OPTIONS? PATCH is used for partial updates. OPTIONS is for show the legitimate methods for particular resource?
Sample code:
<?php
require __DIR__ . '/../vendor/autoload.php';
$mux = new Pux\Mux;
$mux->add('/', [ 'PageController', 'page1' ]);
$route = $mux->dispatch('/');
var_dump($route);
Result with C extension 1.3.1:
array (size=4)
0 => boolean false
1 => string '/' (length=1)
2 =>
array (size=2)
0 => string 'PageController' (length=14)
1 => string 'page1' (length=5)
3 =>
array (size=0)
empty
Pux 1.3.2 Result without C extension:
null
The result without C extension seem to be a bug.
Hi!
We're interested in adding typed function calls to Pux and contributing them back to the project. Taking this example from the docs:
$mux->get('/product/:id', ['ProductController','itemAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ]
]);
would become:
$mux->get('/product/:id', ['ProductController','itemAction'] , [
'require' => [ 'id' => Pux\Mux::TYPE_INTEGER, ],
'default' => [ 'id' => '1', ]
]);
This would automatically match against \d+ and result in a call to as ProductionController()->itemAction( (int) $id) instead of passing a string type.
Why?
This will simplify controller logic and allow invalid input to be sent to a 404 page instead of through controller logic.
It also allows folks to start converting over to Hack / HHVM for other performance gains.
Any concerns over this feature? Will this have any impact on the C extension (which we don't use so I wouldn't be adding any code in).
Add more list_entry type for:
HashTable
route struct
zval (boolean, long, and string)
Hi @mway,
The doc_var_len
is decided by a space char. but what if an user ends the line with \n
not a space? that might return a path like /path\n
?
Line 130 in 40b1c1b
Is it possible to read the Route like Symfony do? because our annotation actually reads this syntax:
class PostController extends Controller
{
/**
* @Route("/")
* @Method("GET")
*/
public function indexAction()
{
// ...
}
}
Just to fully understand and replicate your benchmark, don't you think it would be fair to include the code you've used to test the Symfony Routing component?
For example, did you enable the cache_dir for the Symfony Routing component before running the tests?
https://symfony2-document.readthedocs.org/en/latest/components/routing.html#the-all-in-one-router
Thanks!
Supported formats:
220.120.62.32
192.168.0.1/24
192.168.0.1/32
Is pux a wrapper for C and c++ library as well?
I hate to say it, but comparing a C extension to a pure PHP implementation is like comparing a racehorse against a donkey. Especially for something like this. So saying the extension is faster than the Symphony router isn't saying much.
Personally, I'd like to know where the pure PHP implementation of Pux stands in relation to the extension, as well as other libraries.
That's the error I have: https://gist.github.com/Lunatic666/72484dcbc1faaaddfa9c
In the PHP 5.6 UPGRADE file, they mention that some constants are removed: https://github.com/php/php-src/blob/php-5.6.2/UPGRADING.INTERNALS
1k: k. Removal of IS_CONSTANT_ARRAY and IS_CONSTANT_INDEX hack. There are three occurances of IS_CONSTANT_ARRAY in pux_functions.c. Replace then with IS_CONSTANT_AST and everything works again. I'll provide a fix
So we can implement different strategy in different Pux\Router class.
By running the MuxTest:
./run-test -- --repeat 1000 test/Pux/MuxTest.php
....zend_mm_heap corrupted
Hi @mway,
it seems like we have memory leak in PHP_METHOD(Mux, getRequestMethodConstant)
.
mthit = mthp
is estrdup from req_method. but it's not freed via efree
.
branch: feature/annotation
I think this commit is kind of crucial to the whole default args thing; d547ca8
will there but a new tagged version soon(ish)?
and the README should be updated since it says to set the version in composer to "~1.3" ;-)
to reach the goal of #18, we should first implement the basic functions to store/fetch mux.
currently we've done the pux_store_mux function, now we need to implement pux_fetch_mux.
the API is something like APC, but we store the zval without serialization/unserialization:
if ( ($mux = pux_fetch_mux('app_pmux')) == null ) {
$mux = new Mux;
pux_store_mux('app_pmux', $mux);
}
Currently we're looping all PCRE patterns to match the correct route, which provides a nice flexibility - you can mount sub-routes to a parent one, or dispatch the path in sub-router dynamically.
However in different case, for example: like 100 pattern routes, we should have different dispatching strategy to dispatch routes.
One idea is to Compile all route patterns into one grouped pattern, and the dispatching can be done in one regular expression comparison.
Hi c9s,
My team and I are willing to make a php Framework for a student project and we were wondering under which license is Pux ?
Best regards,
Thomas.
$dispatcher = new APCDispatcher($mux, array(
'namespace' => 'app_',
'expiry' => ...,
));
$route = $dispatcher->dispatch('/request/uri');
Hi @mway
it seems the path for indexAction
is defined to /
by default, however if we mount a controller to something like "/product" we got "/product/", the correct path should be /product
.
I think the default path of indexAction
should be an empty string. :-)
you may run the unit test to see the result:
cd ext/
./compile && ./run-test -- test/Pux/ControllerTest.php
which happens when using pux_store
I would suggest Pux to add support for RFC 6570 which is a comprehensive way for URI templating.
Here's a PHP extension implementation: https://github.com/ioseb/uri-template
And here's the RFC: http://tools.ietf.org/html/rfc6570
Extract some code from RouteExecutor the controller method for sharing the same thing.
I'm getting the following errors trying to compile Pux :
Hi @mway,
I tested the new AnnotationController, and found there is a weird gc bug, which is not shown previously, not sure what changed/forgot to set the refcount? :
https://github.com/c9s/Pux/blob/develop/test/Pux/ControllerAnnotationTest.php#L27
Here is the code that causes gc failed:
// works fine
// $submux = $controller->expand();
// $mux->mount('/product', $submux );
// gc scan bug
$mux->mount('/product', $controller->expand() );
to reproduce this issue , simply run the test cases with --repeat 500
or so:
./run-test -- --repeat 500 --debug test/Pux/ControllerAnnotationTest.php
you might need a zts
enabled + debug
php to trace this issue.
I have a question:
Mux.php
line 241
In a route like $mux->add('/page.html') it matches /page.html but too /page.html1234abcd.
Changing
if ( strncmp($route[1] , $path, strlen($route[1]) ) === 0 ) {
with
if ( strcmp($route[1] , $path ) === 0 ) {
that not happen, responds only the exact match /page.html.
Is there a reason for using strncmp?
To improve even more the performance, you can make create an alias for the Mux class and it be stored in the extension persistent memory.
For web requests, the first request will load from the compiled router file. For subsequent request it can use the persisted object in memory. It will avoid file access to read the compiled file and re-load all the objects for every request.
You can make something like:
$mux = \Pux\MuxCache::load('compiled_router.php', 'app_routes');
// If not stored, include compiled_router and persist it, otherwise just use the persisted object from app_routes alias
\Pux\MuxCache::clear(); // Remove all persisted objects
Something like that. Thoughts?
Hello.
Congratulations for the excellent router!
I am sending a suggestion to improve it in usability for developers (seriously, its a great feature, not in performance, but..it can help for big sites): Allow developer to specify a alias related to a router and reverse the URL based on its alias+options.
Say i have the code that follows:
require 'vendor/autoload.php';
use Pux\Executor;
class ProductController {
public function listAction() {
return 'product list';
}
public function itemAction($id) {
return "product $id";
}
}
$mux = new Pux\Mux;
$mux->add('/product', ['ProductController','listAction']);
$mux->add('/product/:id', ['ProductController','itemAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ],
'alias' => 'product-page'
]);
If i do the following in some place of the code (its great specially for use in templates, but..):
$url = $mux->reverse('product-page', [
'id' => '10'
]);
And $url
will need to have /product/10
string, all with the great speed of Pux. \o/
What you think?
Since we seldom use getRoutes method, we can handle the routes in an efficient way internally. That is, use C struct to save route pcre flag, pattern string instead of looping the zval array and extra zend _index_find
calls.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.