TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Kohana's Best Practices
  • What do you think about writing a guide of Best Practices in Kohana?
    I think Best Practices are the best guides, because you are not only learning to use it, you are learning to use it in the best way.
    And maybe writing them is not so boring and easier than writing documentation.
    And even advanced Kohana users can learn somehing from them.

    I have a first one:
    Delete/comment this from index.php when you are in production:

    if (file_exists('install'.EXT))
    {
        // Load the installation check
        return include 'install'.EXT;
    }
    

    you are saving one if, function call, and disk access in every call to your site.

  • Comment it or delete it.

    /* if (file_exists('install'.EXT)) { // Load the installation check return include 'install'.EXT; } */

  • Very good idea! You might want to call it 'optimalisations'...

    I would be very much interested in forking the guide, and add tips if I come across them!

    I would add headers with 'conditions' when the tips work and when they don't. For example, the above tip obivously only works after you have the site correctly installed and everything works correctly. There might be other conditions for other tips...!

  • well, the bootstrap file is not really the part of kohana. It's more a template for your own bootstrap code. Imo a very lot of kohana users have very customized bootstrap code. Well, my 2 cent:

    if ( ! defined('SUPPRESS_REQUEST')) {
        $request = Request::instance();
        if Kohana::$environment == Kohana::PRODUCTION) {
            try {
                $request->execute();
            } catch (ReflectionException $ex) {
                Log::warning('404 not found: ' . $request->uri);
                Controller_App::do_redirect('');
            } catch (BadRequest_Exception $ex) {
                Log::warning('500 bad request: ' . $request->uri);
                Controller_App::do_redirect('');
            } catch (Exception $ex) {
                Log::error('500 internal error: ' . $request->uri);
                Controller_App::do_redirect('');
            }
        } else {
            try {
                $request->execute();
               echo $request->send_headers()->response;
            } catch (Exception $ex) {
                if (Request::$is_ajax) {
                    $request->status = 200;
                    echo json_encode(array(
                        'type' => 'exception',
                        'trace' => $ex->getTrace(),
                        'exc' => array(
                            'exclass' => get_class($ex),
                            'message' => $ex->getMessage(),
                            'code' => $ex->getCode(),
                            'line' => $ex->getLine(),
                            'file' => substr($ex->getFile(), strlen(DOCROOT))
                        )
                    ));
                }
            }
        }
    }
    

    The ajax error handling can help you to easily debug ajax apps, but furthermore you will need a client-side exception trace displayer.

    Note that this example uses a custom logger.

  • Thanks for the link nickg, we need something like that but Official and well-arranged.
    I think we need to define first some categories (tags at really so each tip can belongs to many categories) like: Performance
    MVC and HMVC pattern
    Database
    ORM
    Helpers
    Errors and Exceptions
    Security

    For example, my first tip (comment install.php) is a performance tip.
    Crystal tip could be of "MVC..." and "Errors..."

    Another tipo for MVC and HMVC:

    // Check if the request is POST
    if (HTTP_Request::POST == $this->request->method())
    

    Another one for Helpers:

    // instead of this:
    isset( array[key] ) ? array[key] : default value here
    // use this:
    Arr::get( array, key, default value here );
    

    It would be useful if the users can comment about the tips (explain them, etc)
    Ideally the tips should be written by the team, I think they know how to use Kohana better than anyone.
    But maybe is possible to let every user to write tips, and we need a rank system here then.
    mmm... is not possible to use StackExchange for this?
    Instead of writing questions the user writes tips of best practices, and uses the tag system (closed, is not possible to create new tags) for marking the tip.
    Each question must have an extra and required tag for Kohana version.
    Other users, instead of writing a reply, they write a comment/explanation/question about the tip.
    I think is possible, what do you think?

  • No, is not possible :(
    I thought StackExchange was open source :(
    http://meta.stackoverflow.com/questions/83591/how-to-run-a-private-stack-exchange-like-site

    At least not the official system, but maybe is possible to use a clone of StackExchange:
    http://meta.stackoverflow.com/questions/2267/stack-overflow-clones

  • There's a Kohana-based Open Source system I'm following right now, as well: Qamini

    Github: https://github.com/serdary/qamini Project page: http://qamini.com/

    Might be worth checking out!

  • It looks really good, small but with enough features to start and written in Kohana, +1 (just practicing :P)
    Can the team put it somewhere so it becomes official?

    I think it needs some little modifications, like:
    restricted access for guests (can't send a tip, and comment a tip)
    better system for tags:
    users can't create new tags, they choose the tags from a list, and they must choose at least a Kohana version tag
    support for code writing (and if possible with syntax color highlight)
    Order tips ("questions") by points and comments
    And of course change the names to everything, there are no more "Questions" now they are "Tips" (?), there are no more "Answers" just "Comments" (and comments can have comments too of course), we don't need the option "Unaswered Questions", etc.

    I think if the team accepts this idea it will be a great and useful project.
    Even tips with negative votes can be very useful to see what we should avoid ! (we can have wrong practices in the same site too :P)
    And this could be a good test for Kohana framework itself.
    And because is written in kohana we can extend their features (and maybe in a future make something like stackoverflow).
    Even more, I think this is something that should be implemented in many situations:
    Best Practices for PHP
    Best Practices for "put any language here"
    Best Practices for "put any framework here"
    Best Practices for "put anything here" :P

    Please vote

  • For Qamini, I think a first step would be to fork it and create specific changes. Changes that are useful on a general level should be pushed back...

    After that, deploy the site, and ask the devs to add a link to it from the forums/homepage/docs.

    However, I'm thinking of it right now, are there no options to use the current docs system? There might not be too many contributors, so using github might not be a problem. The tagging and voting might be useful for every doc-page, as to improve the quality? Adding that would bring great features (in my eyes) to the docs system for all to use...!

    Thoughts?

  • mmm... I want something like stackoverflow :(
    Anyway, I think the team doesn't like too much this idea.

  • You can still go with Qamini :-)

    However, people will need (another) account, especially for the website, while if you use either the docs or stackoverflow.com, people can use existing accounts... Keep that in mind :)

  • or use OpenID like Stackoverflow does, but if the the team develops this "best practices" site, we can use our Kohana account.
    I can't make that site alone, I can't pay a server either :(
    And the team is always repeating the same: don't make your own doc site, contribute to the official one (and I think that's the way to go too).
    I'm not understanding well what do you mean with "current docs system", without tags, voting, ratings, comments, adn search features the "best practices" site is not useful.

  • I meant that if we go with the current docs system, we would need to add tags, voting, ratings and comments... In the end, that would be useful for the other pages as well (not just for the best practices site) so that's a bonus :-) But it takes quite some work...

    Basically, what needs to be done is to discuss with the devs (in IRC?) whether there is the possibility to have such a site (or general QA system like Qamini) in place on Kohana.com. In any case, we could already fork the docs and add some existing best practices to them...?

  • Why IRC? maybe they are not reading this thread, so I will use my mental powers to call them:
    @shadowhand, @zombor, @Isaiah, @samsoir, @Zeelot3k (why Lorenzo is not in the team page?)

  • I meant that if we go with the current docs system, we would need to add tags, voting, ratings and comments... In the end, that would be useful for the other pages as well (not just for the best practices site) so that's a bonus :-) But it takes quite some work...

    Thats what we are currently doing in our kohana-world.com project ;)

  • Sweet! Entered my emailaddress :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion