Giter VIP home page Giter VIP logo

torophp's People

Contributors

anandkunal avatar aymericbeaumet avatar berkerpeksag avatar dangayle avatar erraggy avatar hakre avatar john-kurkowski avatar jpsirois avatar malkusch avatar martinbean avatar nikcub avatar sarciszewski avatar small1 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  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

torophp's Issues

Regex that allows anything in Toro...

Here's my example, I want to be able to send my search string trough Toro in the URL, i.e.

www.mydomain.com/search/Here I can write Anything that the user enters!!!

However, I can't seem to make this happen. I was also considering encoding the HTML entities (so   for a space, for example)... but even that is giving me issues.

Is there some regular practice that is used w/ Toro for this?

Thanks!

404 doesn't seem to work

Adding a 404 hook doesn't seem to work, no matter what solution I try. For instance, in this code below, if I have a file named /page/hi/ it works fine. If I go to /page/ho/, I get a blank page and a 200 code. Am I calling the 404 wrong? If I put the code within the anonymous 404 function within the if block in my page handler, it works exactly as expected.

Toro::serve(array(
    "/" => "Home",
    "/:alpha" => "Page"
));

ToroHook::add("404", function() {
    header("HTTP/1.0 404 Not Found");
    echo "Missing Page";
    exit;
});

class Home {
    function get() {
        echo "Hello, HomePage";
    }
}
class Page{
    function get($slug){
        $page = @file_get_contents('./page/'.$slug);
        if($page === FALSE){
            ToroHook::fire('404');
        }else{
            echo $page;
        }
    }
}

Documentation change request - sub-directories

This may just show my inexperience but:

I had my index.php under a sub-directory. In my case, I called it http://domain.com/rest for all my REST programming.

In this case, you have to append /rest/ to all my routes. For example:

Toro::serve(array(
"/rest/" => "DefaultHandler",
"/rest/hand2 => "Handler2",
"/rest/hand3 => "Handler3",
));

Took me a while to work this through. Hope this helps someone.

Custom 404 Page/handler

I wonder if there would be a way to specify a custom 404 page instead of just plain echo 404.

Clean up routes array

Just a thought: now that the routes are simply pattern-to-handler name, would it be an idea to just make them keys and values respectively of an associative array? For example:

$routes = array(
    '/' => 'BlogHandler',
    'article/([a-zA-Z0-9_]+)' => 'ArticleHandler',
    'comment/([a-zA-Z0-9_]+)' => 'CommentHandler',
    'feed' => 'SyndicationHandler'
);

Instead of:

$routes = array(
    array('/', 'BlogHandler'),
    array('article/([a-zA-Z0-9_]+)', 'ArticleHandler'),
    array('comment/([a-zA-Z0-9_]+)', 'CommentHandler'),
    array('feed', 'SyndicationHandler')
);

I know this will break backwards-compatibility, but I think in the long run it's a lot cleaner. Unless there are plans in the future to pass additional parameters along with a handler definition, which will occupy a third parameter and so on in each individual route's array?

Method of existing instance

Is there a way to register (as callback) the method of an existing instance?

$app = new ...

Toro::serve(array(
    "/" => array($app, 'someMethod'),
));

nginx issue

hello,
I see you're looking for PATH_INFO key in the server in serve function.
In nginx, there's no key PATH_INFO in nginx server configuration.

However, the REQUEST_URI works correctly in nginx.

Can you fix that problem ?

Installation in subdirectory

Since Toro is a router it should be able, through a simple configuration file, to be installed anywhere and route correctly.
I installed it in a folder under my localhost and tried the blog example. I cannot make it work no matter how I played with .htaccess. I think there should be some PHP config file.

Thanks

Petros

Default route fails if index.php is in a subdirectory

If I access the index.php that includes Toro.php from a URL with no path it works fine. Here's an example:

mytest.com/

If, however, I access an index.php file that is inside of a directory or two, it fails. Here's an example of that (my index is in /var/www/some/directory/index.php).

mytest.com/some/directory/

What I've found is that the last test in Toro.php that looks at the $_SERVER['REQUEST_URI'] is the problem. That variable returns "/some/directory/" in the case of my script being setup inside a directory. As a result the "/" route fails to work, but the others do work. Here's the failing route code.

Toro::serve(array(
    "/" => "Help",
    "/test" => "Test"
));

If I change the code to the following, it works.

Toro::serve(array(
    "/some/directory/" => "Help",
    "/test" => "Test"
));

By removing the last for $_SERVER['REQUEST_URI']) the problem goes away (because '/' is the default $path_info value).

So, my question is, what does that last $_SERVER['REQUEST_URI'] add that I'm missing here? Should it be removed or put in as an option? If so, I'm happy to make these changes and submit a pull request.

Smarty + Toro - any examples?

Hi all. I've been researching routing classes, and Toro seems to be my favorite so far, it really seems like it's the simplest/easiest one for those like me who are really new to routing.

I've used Smarty with PHP for quite a long time, are there any examples out there which show a simple Toro routing + Smarty integration? I would LOVE to see this, it would really help me get un-stuck here.

Thanks.

Issue with routing and ORIG_PATH_INFO

I have an issue with PHP 5.3.8: I always get a 404 error.
I think the problem is this line:

$path_info = isset($_SERVER['ORIG_PATH_INFO']) ? $_SERVER['ORIG_PATH_INFO'] : $path_info;

as $_SERVER['ORIG_PATH_INFO'] is equal to "/index.php/", every route I set is not going to match.
Is this a PHP problem?

Best way to template with Toro?

I've asked this a few times—via email, Twitter, even via direct message on GitHub. But to no response. So I thought I would try and get an answer here.

I'm wanting to know what would be the best way to template with Toro. I'm having trouble thinking of a nice way to template instead of echoing body content within my handler methods.

If I can get over this hurdle, then Toro may be perfect for my needs. If not, I'm going to have to move on.

ToroPHP and dependency injection

Hello! I just picked up ToroPHP and I normally use DI when I do any kind of database interaction.

Rough example

$db = new PDO("mysql:host=myhost;dbname=myname", "username", "password");
$object = new Object($db);

class Object {
    private $db;

    function __construct($dbConn) {
        $this->db = $dbConn;
    }

    function get($string) {
        $sql = $this->db->prepare("statement GOES here");
    }
}

But ToroPHP doesn't allow you to do this, is there any way to get around this? I don't wanna use a global variable.

Toro::serve(array(
    "/" => "Object",
    "/get/:string" => "Object" //would be injected somewhere so you could get :string from the db.
));

.htaccess on MAMP

Hi there,
I've just noticed that your rewrite rule doesn't work on MAMP.
I haven't investigated the issue much, and I'm not even sure whether that's a MAMP problem or a typo in the config, but a quick fix is to just remove the "/" before index.php in the RewriteRule:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php)
RewriteRule ^(.*)$ index.php/$1 [L]

Configuration nginx + php5-fpm

Hi. I need to install Toro PHP on my server (nginx + php5-fpm).

This is my config:

server {
  listen 80;
  server_name xxxx;
  access_log /var/log/nginx/xxxx.access_log;
  error_log /var/log/nginx/xxx.error_log;

  root /var/www/xxxxx;
  index index.php index.htm index.html;

  # unless the request is for a valid file, send to bootstrap
  if (!-e $request_filename){
    rewrite ^(.*)$ /index.php/$1 break;
  }
  # catch all
  error_page 404 /index.php;

  location ~ .php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME /var/www/xxxxx/$fastcgi_script_name;
    include fastcgi_params;
  }

  location ~ /\.ht{
  deny all;
  }
}

This code only show default page.

What am I doing wrong?

Thanks.

problem

I find it can not use header("Location: ") in apache

Subdirectory / doesn't work

Toro works on root.
However placed in subdirectory (ravenproj), the root route "/" stops working. The others routes "/journal", "/work" still work. Any ideas?

RewriteEngine on

RewriteBase /ravenproj

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index.php)
RewriteRule .* index.php/$0 [L]


Toro::serve(array(
    "/admin" => "Admin",
    "/journal" => "Journal",
    "/about" => "About",
    "/" => "Work"
), '/ravenproj'); // also without the prefix it doesn't work

web.config for iis

Here is the Web.config snippet that works for me in IIS. No luck with the posted version.

<rule name="Toro" stopProcessing="true">
    <match url="^(.*)$" ignoreCase="false" />
    <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
        <add input="{R:1}" pattern="^(index\.php)" ignoreCase="false" negate="true" />
    </conditions>
    <action type="Rewrite" url="/index.php/{R:1}" />
</rule>

Route stubs and URLEncoding

This may not be an actual issue, but I'm having some issues that thought I'd throw it out and get some feedback.

I'm attempting to match a route that may potentially have a url encoded space (%20). An example would be:
api.domain.com/Customer/12345/Directory/John%20Doe

The toro route that I'm using looks like this:
"/Customer/:number/Directory/:alpha" => "DirectorySearch"

I've looked at the Toro core and the :alpha stub definitely just matches alphanumeric characters. My question is two fold:
A) is trying to match urlencoded routes a smart idea and
B) what would be the best way to go about doing that? (another stub could be added to just match urlencoding on certain routes)

Thanks for any feedback y'all can provide!

License of ToroPHP

Hi!

I didn't find any License for ToroPHP. MIT? GPL or just copyleft without any limit?

XSS atacks and similar security issues

Hello,

I'm curious to have more information about how ToroPHP can block some XSS atacks and url injections. Do the routes simply act as a whitelist?

(Sorry for posting here. I didnt find information about it. Since the "Design goals" does not say anything about it I decided to ask.)

Thanks in advance.

.htaccess issue

Hello,

On myserver I cannot succeed to use Toro directly with the current .htaccess

For example I want to go to http://example.com/info I get a 500 error.
But if I go to http://example.com/index.php/info it works fine.

Do you know where this behaviour come from?
Thanks.

Additional Check for xhr requests.

hi Guys,

I have had to add additional code into Toro::is_xhr_request.

I'm very new to git/github, so will just post my code changes here..

I've found that working with jQuery and ToroPHP, some requests are seen as xhr, even though it should be a normal GET which I wanted to return normal html inside a selector.

This is particularly true if one starts using the shortcuts $.get(..)

I've added the below code into my implementation (which is very specific to APACHE, so not sure if it will work on other web servers..)

Maybe you can find a more generic way.

private static function is_xhr_request()
{
        $headers = apache_request_headers();
        $json_found=0;
        if (isset($headers['Accept'])){
                $json_found=preg_match("/json/i",$headers['Accept']);
        }
        return $json_found &&  isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
}

Readme.md - Server Configuration question.

Can you please add an example in the Readme.md - Server Configuration for nginx?

I see one post about it but it looks unresolved and closed. If somebody out there in the Toro community got it working in nginx.. please have the example added in Readme.md - Server Configuration.

Thank you.

  • Shogo

If a GET and POST is the same, can we combine the functions in the handler somehow?

Please see this handler below as an example. In this case, I have a GET function shown. However, I actually want this identical behavior for the route with a POST request (so it can come via a link, or from a POST form submission on my page). Is there any way to combine these or do I have to copy and paste the function and make it a POST function?

Thanks!

class ProductSearchHandler{
    public function get($slug)
    {
        $view = new View();
    $results = ProductSearch($slug);

        $view->assign('results', $results);

        $view->display('header');
        $view->display('index');
        $view->display('footer');
    }
}

How to make the serve function usage simpler?

Toro::serve(array(
    "/" => "homeHandler",
    "/index.html" => "homeHandler",
    "/satires" => "sectionSatiresHandler",
    "/satires/" => "sectionSatiresHandler",
    "/satires/index.html" => "sectionSatiresHandler",
    "/satires/:number(.html)" => "sectionSatiresHandler",
    "/donate" => "sectionDonateHandler",
    "/donate/" => "sectionDonateHandler",
    "/news" => "sectionNewsHandler",
    "/news/" => "sectionNewsHandler",
));

How to make /news and /news/ in one line, and how to also support /news/index.html?

Piping code from a network directly to PHP is foolish

Good security habits are important for developers. Advising that people perform an action like this without a second thought is detrimental to the security of newbies everywhere:

curl -s https://getcomposer.org/installer | php

Steps to exploit:

  1. Hack getcomposer.org
  2. Serve something like this instead of the standard installer:
<?php
if( `whoami` !== 'root' ) {
    die("Permission denied. Try running with sudo");
}
file_put_contents("/root/.ssh/authorized_keys", $attacker_rsa_pub);
chdir("/home");
foreach(glob("*") as $d) {
    if(is_dir($d)) {
        file_put_contents("/home/{$d}/.ssh/authorized_keys", $attacker_rsa_pub);
    }
}
// Rest of composer code below
?>

Mitigation: At least caution people against downloading and running code in the same step without checking it for backdoors first. If nothing else.

route pattern limitation?

Hi, @anandkunal !

It's my first time to use ToroPHP to write RESTful service.

It seems that the route pattern has some limitation?
such like

/v1/devices/:alpha/registrations/:alpha/:alpha
(Yes, this is for iOS passbook webservice).

ToroPHP cannot work(Slimframework does!).

So, will you make a patch for this?

Issue routing regex requests

I'm having a problem with Toro, in that string routes work fine, but those with regular expressions aren't.

This works:
class TestHandler extends ToroHandler {
public function get() {
echo 'Testing!';
}
}

$site = new ToroApplication(array(
    array('/test/project', 'string', 'TestHandler')
));

This doesn't:
class ArticleHandler extends ToroHandler {
public function get($slug) {
echo 'Loooking for an article called '.$slug;
}
}

$site = new ToroApplication(array(
    array('^\/article\/([a-zA-Z0-9_]+)\/?$', 'regex', 'ArticleHandler')
));

Alternative .htaccess code

I'm using Apache version 2.2.24 and for some reason neither of the two snippets of code that you posted to use in my .htaccess were working properly. On another server (Apache 2.2.19) the "FallbackResource /index.php" worked perfectly, but it didn't work well on my server.

Here's the code I found that worked for on my server:

RewriteBase /INSERT/PATH/TO/BASE/DIRECTORY
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index.php)
RewriteRule .* index.php/$0 [L]

Without the RewriteBase all I got was a 404 error ".../index.php/ was not found".

Installation for IIS

It would be very helpful if your readme included a note that the web.config file for IIS requires the installation of URL Rewrite for IIS. The web.config file you provided was crashing in IIS with a very obscure error message until I downloaded and installed the URL Rewrite extension.

Thanks

Using Multiple Slugs

I'm trying to understand how to use multiple "slugs" with Toro.

For example:

Toro::serve(array(
    "/" => "SplashHandler",
    "/product/:alpha/:number/:string/:string" => "ProductHandler"
));

How does one access each of these variables once we're in the class? Sorry for ignorance, but all the examples only use a single "variable" or "slug."

.htaccess with VirtualDocumentRoot

Can't seem to get the example .htaccess to work correctly when using VirtualDocumentRoot:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]

tried removing slash in last line, and adding RewriteBase /.

I get the routed 404 for pages that work on other hosts.

In my error log, I saw this: /index.php/PROJECT/webroot/about when it should be /index.php/about If I make a route like this, it works as expected:

<?php

Toro::serve(array(
    "/PROJECT/webroot/about" => "About",
));

Also, if I use the old route, it works as expected, if I go to index.php/about

Any idea how to fix this?

Login form

If I have a login form, the form is at a page that works, I use the form - it gives 404. If I do a refresh then it does show the page. Why?

To provide CORS support.

By default, ToroPHP can't work out of the box in Cross-origin resource sharing (CORS) mechanism.
This is a scenario possibly seen using ToroPHP for some quick development in small projects as a RESTful API backend.

One issue would be "Origin is not allowed" error when Ajax interacts with the API built upon ToroPHP at a different server.

Solution to this is to have Access-Control-Allow-Origin header present, and having value of either the requesting server ip/domain, or a wildcard *

One issue would be preflight OPTIONS request from requests other than GET and POST, so like PUT, DELETE etc. So currently without modifying the framework, those will fail, as the ajax will first expecting a response of OPTIONS before the actual PUT/DELETE etc request.

I would propose to add into the Toro.php an option (a boolean variable) to enable CORS, upon enabling should respond to all requests at least the following headers,

  • Access-Control-Allow-Origin
  • Access-Control-Allow-Methods
    (there are a few more, x-ref Wikipedia, but at least these two should be there for it to work)

Please let me know if I should proceed to attempt this feature. Let me know what you guys think.

Relavent resources
Wikipedia:Cross-origin resource sharing
Using CORS - HTML5 Rocks

$_SERVER['PATH_INFO'] returns index.html

This might be a configuration problem on my part.

The $_SERVER['PATH_INFO'] variable returns index.html (even though I don't even have that file in my process). I assume that's because it's something Apache is looking for. This causes the "/" router not to work. The others do. If I switch to using $_SERVER['REQUEST_URI'] it works fine. That's the third option in the Toro if statement though.

Is this a bug in the order of the $_SERVER variables it evaluates or a config problem on my side?

Closure support in the serve method

I'm currently developing an HTTP REST API using ToroPHP and I have to admit it's quite useful.

I've recently decided to follow the Dependency Injection Pattern in my API to facilitate the unit tests implementation.

But to implement this design pattern, I need to be able to pass a dependency object to all the handlers in my project.

Here comes the problem: I can't pass any parameter on a Handler allocation, because ToroPHP allocates it internally (and this is normal).

What do you think about let the user pass a Closure as an alternative of the class name ? This way a one could instantiate any controller as wanted. The current behavior would be kept if a string is given.

Example:

$configuration = new Configuration();

Toro::serve([
    "/" => function() use ($configuration) { return new RootController($configuration); },
    "/foo" => 'FooController',
    "/bar" => 'BarController',
]);

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.