Code Monkey home page Code Monkey logo

dispatch's People

Contributors

4d47 avatar dasrecht avatar devside avatar kafene avatar kanti avatar lloydzhou avatar martinaglv avatar nmcgann avatar noodlehaus avatar oschettler avatar rmasters avatar scan avatar wtmissions avatar yalp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dispatch's Issues

Collision between route parameters and query string parameters

If a route defined like "/edit/:aval" has a query string parameter with the same name as the named parameter then the query string parameter is overwritten by the route parameter.

e.g. with the example above if the URI is /edit/1234?aval=5678 then a call to param('aval') returns 1234 as the QSP of the same name with value 5678 is overwritten.

Problem originates in the param() function called in the on() function:

 // used by on() for merging in route symbols.
$source = array_merge($source, $name);

The merge overwrites GET/POST symbols with the same key name.
(I added a separate rparam() function in my application and kept the route params completely separate to GET/POST)
If this limitation is intentional then it would be useful to add a note in the user guide warning people to choose non-colliding names.

'Before' does not run on every route

I have put the json-Example on my dev-system to work, with the appropriate changes. Source looks like this:

<?php

require_once __DIR__ . '/../vendor/autoload.php';

config('source', __DIR__ . '/../config.local.ini');

before(function () {
    var_dump('#moo');
    scope('object', array('name' => 'Dispatch', 'type' => 'Framework'));
});

on('GET', '/json', function () {
    json(scope('object'));
});

on('GET', '/jsonp', function () {
    json(scope('object'), 'callback');
});

dispatch();

I had to notice that the scope('object') returns null on the first route (/json), but works fine on the other one (/jsonp). I added the var_dump in case this was a problem with scope, but the dump does not appear when calling the /json route. I'm using the dev-master version from composer. The config.ini only sets a dispatch.url, which I don't think matters here.

Issue loading views/layout using render()

Hello.

I'm not quite sure about whats the difference between "layout" and "view". In the render function the first parameter is ignored except when the third one is set to false.

Exactly backwards what is wrote on the documentation.

for example, if i execute render('view', array()); the layout loaded is "layout.html.php" in the views directory not the view.html.php as it's expected. Right?

To change the file included from the views folder, i have to set the third parameter, making the first one, even more useless.
render('view', array(), 'custom_view');

I'm not sure, if this is the expected behavior or not. If it is, the docs are not correctly explained, and i'm the first one who wanna know whats the difference between "view" and "layout".

Thanks!

Feeding `on` with names of undefined functions kills router silently

It's a quite simple problem I notices when I was refactoring my code. I changed the closures from

on('GET', '/bla', function () {
    // Hic sunt ponies
});

to

on('GET', '/bla', 'getBla');

Suddenly my router gave me a 404 for every URL. After trying every call of on separately, I found out that when you feed the function the name of an undefined function (typo, import error), the router will only stop adding new routes beyond that point and will give only 404.

It would be much better if the function would output some kind of runtime error to make debugging this easier.

I will perhaps look into the issue later when I have the time.

Parse error: syntax error '['

Hi,
there is a problem with php 5.3 and Array declaration in Dispatch. Especially at line 22 and line 490 you define an array variable with '[]' but this cause an error in PHP < 5.4. Can you fix this?
Great Job!
Thank you

/ route does not seem to work

hi there

i currently move a small redirector app from dispatch 1 to dispatch 2

i try to map the /index route but it always gives me a 404 Page not found back

I'm using the example code in the page. do you have an idea? when i call

redir.lo the 404 gets out
redir.lo/index and i hit the defined on route.

I think we had some magic to use the /index route as / right?

/bastian

flash

God Domingo for Dispatch. I like your microframework very much, it let me like to write the code on nodejs. Thx very much.
PHP Version 5.3.6
I find it can't dipsay flash example correctly.
So I print the cookie '_F' and found out the quotes in json string from cookie are slashed. ex: " '.. it leads to the json_decode returns null.
I try to modify the dispatch code, add 'stripslashes', to fix it out, sorry it failed.
Did you meet this error before?

Call to undefined function finfo_open()

Fatal error:  Call to undefined function finfo_open() in \vendor\dispatch\dispatch\src\dispatch.php on line 371

Seems to be an issue that my PHP instance does not have fileinfo installed...

$ php -v
PHP 5.4.12 (cli) (built: Feb 21 2013 17:44:16)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
phpinfo()
Version PHP 5.4.12
Build Date Feb 21 2013 17:41:51
Compiler MSVC9 (Visual C++ 2008
Architecture x64
Configure Command
cscript /nologo configure.js
"--enable-embed"
"--enable-cli-win32"
"--enable-apache2-2handler"
"--enable-apache2-2filter"
"--enable-apache2-4handler"
"--with-mysql=shared"
"--with-mysqli=shared"
"--enable-pdo"
"--with-pdo-mysql=shared"
"--with-pgsql=shared"
"--with-pdo-pgsql=shared"
"--with-mcrypt=static"
"--with-openssl=shared"
"--enable-sockets=shared"
"--enable-intl=shared"
"--enable-mbstring=shared"
"--enable-mbregex"
"--enable-exif=shared"
"--with-xmlrpc=shared"
"--with-xsl=shared"
"--enable-solr=shared"
"--enable-solr-debug"
"--with-curl=shared"
"--with-tidy=shared"
"--with-bz2=shared"
"--enable-rar=shared"
"--enable-fileinfo=shared"
"--with-gettext=shared"
"--with-mhash"
"--with-ldap=shared"
"--enable-com-dotnet=shared"
"--enable-soap=shared"
"--enable-shmop=shared"
"--with-gmp=shared"
"--with-interbase=shared"
"--with-pdo-firebird=shared"
"--with-sqlite3=shared"
"--with-pdo-sqlite=shared"
"--with-pdo-odbc=shared"
"--enable-dbase=shared"
"--with-pdo-oci=C:\php-sdk\oracle\x64\instantclient_10_2\sdk,shared"
"--with-oci8=C:\php-sdk\oracle\x64\instantclient_10_2\sdk,shared"
"--with-oci8-11g=C:\php-sdk\oracle\x64\instantclient_11_2\sdk,shared"
"--with-sybase-ct=shared"
"--enable-couchdb=shared"
"--with-couchbase=shared"
"--enable-mongo=shared"
"--with-imap=shared"
"--enable-mailparse=shared"
"--enable-pop3=shared"
"--with-smtp=shared"
"--with-oauth=shared"
"--with-ssh2=shared"
"--with-snmp=shared"
"--enable-uploadprogress=shared"
"--enable-http=shared"
"--with-imagick=shared"
"--enable-discount=shared"
"--with-pdflib=shared"
"--with-haru=shared"
"--with-excel=shared"
"--with-enchant=shared"
"--enable-printer=shared"
"--with-geoip=shared"
"--enable-timezonedb=shared"
"--with-xdebug=shared"
"--enable-suhosin=shared"
"--disable-optimizer-plus"
"--enable-pthreads=shared"
"--enable-pthreads=shared"
"--enable-win32service=shared"
"--with-memcached=shared"
"--enable-memcache=shared"
"--enable-apc=shared"
"--enable-apc-srwlock-native"
"--enable-apc-debug"
"--enable-xcache=shared"
"--enable-xcache-optimizer"
"--enable-xcache-coverager"
"--enable-eaccelerator=shared"
"--enable-varnish=shared"
"--enable-ffmpeg=shared"
"--disable-security-flags"

Look into allowing arguments to be passed to `next(...)` from within the middleware

Right now, middleware functions have no way of forwarding values down the chain, except via stash(...). It might be good to allow value passing via the next(...) call.

function jwt_verify(callable $next) {
  # ...
  $data = jwt_decode(...);
  return $next($data);
}

This passed parameter then gets appended to the list of args that gets passed down the chain. Succeeding middleware and handlers can then access this additional parameter from the tailing ...$args.

mod_rewrite required?

Great job!

But as far as is understood the library, Apache mod_rewrite is required and I haven´t seen any documentation stating this subject and a simple example of a .htaccess file.

If I'm wrong and apache mod_rewrite is not required, how does the library process the requests that would throw a 404?

For example:
A request for http://site.com/a/b/1
Apache will try to find a subdirectory in the virtual root directory with the path "/a/b/1".
How does dispatch get this request, if the code with the mappings is located in the file "/index.php"?
Thanks.

Allow mapping of handlers against error codes (eg. 4xx, 5xx)

Right now, there's still no way to specify handlers that get executed whenever:

  1. serve(...) doesn't find a route match (404)
  2. serve(...) encounters an error or exception during handler execution (500)
  3. an action handler wishes to return (client/server) error code to trigger the error page

Probable approach for this would be to:

  1. Allow error status codes as first argument to apply(...), eg. apply(404, page('error404'))
  2. Introduce a new function, maybe trap(404, page('error404'))

For question problem 3, maybe use something like return error(422, 'not-processable-template') within handlers for triggering error codes.

Dispatch 10.2 seems really slow

Running Dispatch in my local dev environment using PHP's built-in server causes ~1+sec of latency to the page response. Just switched a few of my routes over to https://github.com/bramus/router and now I'm seeing page response times on those routes of ~330-560ms.

The only change I made was the router library running things, everything else in my app, IO, DB, template rendering, etc was unchanged.

I'll see about coming up with a couple of test cases to validate this, but I'm curiuos if Dispatch's internal route management is just inheritely inefficient?

The Road To Dispatch 5

I will try to get work on that done during the weekend.

Dispatch 5 will change the API entirely and move from global functions and closures to singletons and proper OO for routing.

The main elements of dispatch for me are:

  • Easy to set up (No config, no special directory structure required)
  • Speed
  • No MVC
  • Speed
  • Retains compatibility with 5.3 but uses everything 5.3 has to offer
  • Speed
  • Painless integration of often-used elements.

This issue is to collect some ideas and discuss them. Please wait for the code draft in my Repo before commenting on implementation.

send() causes Out Of Memory with any file of any size

I have a the problem that I can't make a file download path. The error I'm getting is this:

PHP Fatal error:  Allowed memory size of 8388608 bytes exhausted (tried to allocate 7344128 bytes) in vendor/dispatch/dispatch/src/dispatch.php on line 392

It doesn't matter how I set my memory limit, the function always exceeds it. I tested with up to 128M. My code is similar to this:

on('GET', '/file', function () {
    nocache();
    $db = scope('db');
    $file_id = params('id');

    $row = $db->files()->where('id', $file_id)->limit(1)->fetch();

    if($row === false) {
        error(404);
    } else {
        send($row['file_path'], $file_id . '.bin');
    }
});

Using dev-master of dispatch and the only other library in use is notORM.
I checked that $row['file_path'] exists. The smallest file I have tested it with has 500 KB, and it still occurs. Maybe I did wrong in the call?

Is there a way to use it inside a folder?

Hi!

I need to use the application inside a folder on the server (ex: \public_html\app), but it has 404 errors. It works normally at the root, and I can't use it on the root because it conflicts with the main app (made with Laravel).

I saw in previous releases that there was a config to support this function, but even adapting the code to use an old version, I was not successful.

Does anyone have a tip on how to make it work on a folder?

Below is a snippet of the code:

##index.php##
<?php
    require 'lib/dispatch.php';
    require 'mysqldbadapter.php';


    route('GET', '/', function ($db) {
        header('Location: /index.html');
    });


    route('GET', '/getActive', function ($db) {
        $json = json_encode($db->getSurveys());
        return response($json, 200, ['content-type' => 'application/json']);
    });
    
    $db = new MysqlDBAdapter();

    dispatch($db);


##mysqldbadapter.php##

<?php
    class MysqlDBAdapter { 
        private $dbh;

        public function __construct() {
            $this->dbh = new PDO("mysql:host=127.0.0.1;port=3306;dbname=user;user=root;password=123");
        }

        public function getObjectFromStorage($storageId) {
            $sqlQuery = 'SELECT * FROM ' . $storageId;
            $data = array();
            $sql = $this->dbh->query($sqlQuery);
            while($result = $sql->fetch(PDO::FETCH_ASSOC)) {
                $data[$result['id']] = $result;
            }
            return $data;
        }
    
        public function getSurveys() {
            return $this->getObjectFromStorage('surveys');
        }
    }

before()

Hi, quick question,
what is the alternative to the before() method in the latest release?

Site in a sub-directory keeps redirecting to root

I have deployed a dispatch site at root.com/sub/ but it's keep redirect to root.com. I'm using nginx as a web server and it's config is correct. I tried echoing a "hello world" from root.com/sub/index.php it works but doesn't work with dispatch.

move tests to PHPUnit

need to use PHPUnit instead of adhoc tests. might need code changes as well, make things more testable.

Note: .less files aren't accessible due to .htaccess restriction

More of a note for anyone trying to leverage less.js in the browser for their dev environments. Make this change in your projects root .htaccess file:

<IfModule mod_rewrite.c>
  RewriteEngine on
  # in this case, our app bootstrap file is index.php
  RewriteRule !\.(js|html|ico|gif|jpg|png|css|less)$ index.php 
</IfModule>

Adding |less to your rewrite rule will allow the server to deliver .less files to the browser.


Edit: The same thing goes for any mime types that aren't whitelisted by this rewrite rule.
eg: mp3|mustache|handlebars|json|jpeg|ogg|mp4|wav|wmv etc.

Although the download() function is better suited for media formats, and a proper endpoint that the backend could facilitate for template dumps to ajax requests wouldn't be out of the question.

Can I set optional parameters in route?

map('/quiz/question/{action}/{?id}', function ($args=null)

I want the id to be an optional parameter in the url, but it isn't working. Also how do I retrieve the queries in the url like site.com/quiz/question/edit?id=123 where I want to retrieve id. I tried params['id'] and args['id'].

route to '/'

I can not do route to addresses '/'

include __DIR__.'/vendor/dispatch/dispatch.php';
get('/', function(){
    render('index', null, false);
});
dispatch();

do not work for me.

Seems url parameters break routes

Trying to add url arguments to a route, and it breaks the route handling.

<?php
  // http://localhost:3005/login?wafklds > works
  // http://localhost:3005/login?wafklds=wjdfkls > doesn't work

  map('GET', BASE_URL.'/login', function() {
    echo 'login';
  });

Array declaration error with PHP 5.3.13

When executing against PHP 5.3.13, the array declaration using the brackets syntax [] provoke the error :

Parse error: syntax error, unexpected '[' in (...)

Replacing [] by array() should resolve the issue.

(Proposal) If a filter returns a value, pass that to on() callback

Basically I want something like this:

filter('task', function($taskId) {
    return Task::find($taskId);
});

// GET /tasks/123
on('GET', '/task/:task', function($task) {
    // Passed value is the result of the filter
    assert(is_null($task) || $task instanceof Task);

    // Original value still available in params(): 123
    params('task');
});

The downside - we can have more than one filter per param - I'm not sure what the best solution would be for that. I guess either:

  1. Passing the result of one filter to the next, or the original value if nothing is returned (only makes sense if every filter returns a value),

    foreach ($filters as $symbol => $callback) {
        $res = call_user_func($callback, $val);
        if (!is_null($res)) $val = $res;
    }
    
  2. Passing the original value to each filter, and passing an array of filtered values if more than one filter returns something.

    // Get the results of the filters
    $results = [];
    foreach ($filters as $symbol => $callback) {
        $res = call_user_func($callback, $val);
        if (!is_null($res)) $results[] = $res;
    }
    
    // Decide what to pass to the on() callback
    switch (count($results)):
        case 0:
            // No filters returned values, pass original value
            $values[$symbol] = $val;
            break;
    
        case 1:
            // 1 filter returned a value, pass this on it's own
            $values[$symbol] = array_shift($results);
            break;
    
        default:
            // More than 1 filter returned a value, so pass an array
            $values[$symbol] = $results;
            break;
    }
    
    ...
    call_user_func_array($on, $values);
    

I'll probably hack something up, but do you have any thoughts on this?

Custom page titles in layout.phtml?

I see the given example of a blog is working perfectly, but is there any way I can put custom "title" tags which is defined in dispatch/example/views/layout.phtml. How can I put $post['title'] from dispatch/example/views/post.phtml in the header title tags instead of a common title for all pages?

In the same way, I want to put custom keywords and description meta tags. How can I pass these variables from post.phtml to layout.html?

Dispatch route into sub directory

Hey there,

if I'm using dispatch in an subdirectory like this: example.com/dispatch-test/ It won't accept

map('/','handler');

I must do it like that:

map('dispatch-test/','handler');

Is this the only way of doing this, or is there a smarter way?

site_url() function

Hey guys,

where has the site_url() function gone? I can't find it in the latest version!?

Cheers,

Simon

namespace

Please add Namespace in dispatch.php

Reason
Avoid function name conflict.
Right now we can not create another function with the name that already exists in dispatch.

Solution
Add Namespace

// dispatch.php
<?php
namespace Dispatch;
?>

Usage

<?php
include '../../src/dispatch.php';
use Dispatch as app;

app\on('GET', '/index', function () {
  echo "GET /index invoked";
});

app\dispatch();
?>

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.