Before diving into Nette, you should have a basic understanding of PHP and how web servers operate. It's also recommended to have a PHP development environment installed, such as XAMPP or Docker, and a code editor like Visual Studio Code or PhpStorm.
Installing Nette
The easiest way to get started with Nette is through Composer, a dependency management tool for PHP. Open your terminal and run the following command:
composer create-project nette/web-project <project-name>
This command will create a new project with a pre-configured directory structure and all necessary dependencies.
Project Structure
After installation, let's familiarize ourselves with the project structure. The main folders we'll focus on are app
, log
, temp
, and www
. The app
folder contains all the application source code, including presenters, components, and templates. log
and temp
are for logging and temporary files, respectively. Publicly accessible files, such as CSS and JavaScript, can be found in the www
folder.
Configuration and Routing
Another crucial step is configuring the application and routing. Application configuration is done in .neon
files located in the app/config
directory. Here, you can set up things like database connections or services your application utilizes.
Routing determines how URL addresses are mapped to presenters and actions in the application. Nette allows you to define routes both in .neon
file formats and directly in PHP scripts.
Creating Your First Page
To create your first page, create a file named HomepagePresenter.php
in the app/Presenters
directory. This file should contain a class HomepagePresenter
that extends Nette\Application\UI\Presenter
. Inside this class, define methods representing various presenter actions.
namespace App\Presenters;
use Nette\Application\UI\Presenter;
class HomepagePresenter extends Presenter
{
public function renderDefault()
{
$this->template->anyVariable = 'Hello, Nette!';
}
}
Templates
For each action, you can create a template that defines the appearance of the page. Templates are typically located in the app/Presenters/templates
directory and have a .latte
extension. For the above-defined renderDefault
action, create a template Homepage/default.latte
:
<!DOCTYPE html>
<html>
<head>
<title>Nette Application</title>
</head>
<body>
<p>{$anyVariable}</p>
</body>
</html>
Testing the Application
After completing the above steps, you should be able to run your application and view your first page. If you're using the built-in PHP server, you can run the application with the command:
php -S localhost:8000 -t www
Open your browser and navigate to http://localhost:8000
. You should see your first page with the greeting "Hello, Nette!".
This article provided a basic overview of how to get started with application development in the Nette Framework. There are many more features and capabilities that Nette offers, so we recommend exploring the documentation and continuing to learn to further enhance your Nette skills.