Wiki
Clone wikikhk-independent / Basic setup
We are working in (Ubuntu) Linux using JetBrains PhpStorm IDE, Apache2, PHP5.4 and MySQL.
This guide uses the codename jolly
for the project.
Rules
- When you code: everything except the text visible to the user is in English: variables, file names, comments, commit messages...
- Commit often with meaningful commit messages
- Write comments for your code. Tip for PhpStorm: automagical PHPDoc
IDE
- Download and install PhpStorm Note: This is professional commercial software and using it beyond 30 days requires a license. If you are starting to reach this limit, ask for the license in Skype channel (we have a valid, legit educational license for you).
Framework
- Download the Kohana Framework files, unzip and put it under
/var/www
. Rename the folder tojolly
(so yourindex.php
is located at/var/www/jolly/index.php
. - Open PhpStorm and choose Open Directory, browse to your project and open it.
- Create a new file in your project root:
.gitignore
. Ignore cache, logs and.idea
directory contents. - Create a new GitHub repository for the project
- Initialize a new local git repository and do a initial commit for the framework files. GitHub gives you instructions for this. Make sure you do this in the project root (
/var/www/jolly
)! - Open the project in your browser: http://localhost/jolly. You should see the Kohana Install view. Fix errors shown by the Kohana installer. Most likely you'll have to
chmod
logs
andcache
directories so that Apache can write to it. - When the screen turns green delete
install.php
.
Note that you'll only change files in the application
directory. Files under modules
and system
are not for editing!
- Rename
example.htaccess
to.htaccess
. ChangeRewriteBase
to/jolly/
.
Renaming in PhpStorm: Right click on the file, Refactor -> Rename. Familiarize yourself with the IDE and its shortcuts and options.
- Open
bootstrap.php
, changebase_url
to/jolly/
. - Open http://localhost/jolly/welcome/index. You should see "hello, world!". If not, check that Apache module rewrite is enabled and
.htaccess
files are enabled in the default virtualhost.
The Kohana framework is now ready for use.
Don't forget to do regular commits. Commits should happen after each change that can be "chunked" to be independant from other changes. The general rule is as often as possible. If you need to use the period in your commit message it's a sign that there are several changes in one commit. Use PhpStorm or the command line for commits.
Database
- Install and open PhpMyAdmin: http://localhost/phpmyadmin. Log in with the root user.
- Create a new user
jolly
and a database for that user, also calledjolly
. Don't forget to allow the user access to that database and that database only (no global permissions!) - Copy
modules/database/config/database.php
toapplication/config/database.php
and write your database access info into the default configuration inside that file. - Write
Cookie::$salt = 'my-random-32-char-string';
intoapplication/bootstrap.php
. This is required to use cookies. Use a password generator for this, I like passwd.me. - Open up the database in PhpMyAdmin and import
modules/orm/auth-schema-mysql.sql
into the database. You should see some role and user handling tables appearing, great! - Enable
ORM
,database
andauth
modules.
The HTML controller
You should still see "hello, world!" when viewing the project in a browser. We want to display some HTML (a template) with changing content area, for that we need to modify the default controller.
We'll be working in the application/classes/Controller
dir.
- Create a new controller:
Main.php
. The main controller extendsController_Template
. All our controllers (except the login one) will be extending this. - Rename the
Welcome
controller toDash
. Don't forget that the class and file names must match for autoloading to work as described in the Kohana documentation. - Make the
Dash
controller extendController_Main
- Change the default route in your
bootstrap.php
. We changed our default controller name fromwelcome
todash
. - Delete
$this->response->body('hello, world!');
from the index action body - Create
views/template.php
. This is where your HTML master template goes, just copy-paste a Bootstrap example into it. Pro tip: remove all the unneeded parts. The JS, favicons etc. - Open http://localhost/jolly/dash. You should see rendered HTML from your
views/template.php
file. It looks messy tho (unstyled). That's because there is no CSS file loaded. Let's fix that. - Head over to Bootstrap CDN. Copy-paste the "Complete CSS (Responsive, With Icons)" HTML line and put it in the
<head>
of your template.
You should now have a working and rendered HTML template. My screen looks something like this:
One more thing tho: dynamic content (for each controller/action we want different content, but the same wrapper or the template.
- Create:
application/views/dash/index.php
. Write some content in it: `I am the dash/index view!
- In your
Controller_Dash
, inside the index action (where the$this->response->body()
line was), write:$this->template->content = View::factory('dash/index');
- In your template file (
views/template.php
) locate the part where the main content goes. If you used the minimalistic starter template, it's inside<div class="container">HERE</div>
- Echo the variable
$content
in that place:<div class="container"><?=$content?></div>
- Refresh the page - you should see the index view content rendered in your template.
Well done, you have a basic Kohana setup with database and HTML template support. Move on to the next part: Implementing Login.
Moving on
Next up: Implementing login
Appendix
The Dash controller
<?php defined('SYSPATH') or die('No direct script access.'); class Controller_Dash extends Controller_Main { public function action_index() { $this->template->content = View::factory('dash/index'); } }
Updated