Skip to content

change README versions for Nette 3.0 #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: v3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
1 change: 1 addition & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Require all denied
24 changes: 24 additions & 0 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

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

$configurator = new Nette\Configurator;

//$configurator->setDebugMode('23.75.345.200'); // enable for your remote IP
$configurator->enableTracy(__DIR__ . '/../log');

$configurator->setTimeZone('Europe/Prague');
$configurator->setTempDirectory(__DIR__ . '/../temp');

$configurator->createRobotLoader()
->addDirectory(__DIR__)
->register();

$configurator->addConfig(__DIR__ . '/config/config.neon');
$configurator->addConfig(__DIR__ . '/config/config.local.neon');

$container = $configurator->createContainer();

return $container;
7 changes: 7 additions & 0 deletions app/config/config.local.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:


database:
dsn: 'mysql:host=127.0.0.1;dbname=test'
user:
password:
20 changes: 20 additions & 0 deletions app/config/config.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
parameters:


application:
errorPresenter: Error
mapping:
*: App\*Module\Presenters\*Presenter


session:
expiration: 14 days


security:
users:
admin: secret # user 'admin', password 'secret'


services:
router: App\RouterFactory::createRouter
27 changes: 27 additions & 0 deletions app/presenters/Error4xxPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\Presenters;

use Nette;


final class Error4xxPresenter extends Nette\Application\UI\Presenter
{
public function startup(): void
{
parent::startup();
if (!$this->getRequest()->isMethod(Nette\Application\Request::FORWARD)) {
$this->error();
}
}


public function renderDefault(Nette\Application\BadRequestException $exception): void
{
// load template 403.latte or 404.latte or ... 4xx.latte
$file = __DIR__ . "/templates/Error/{$exception->getCode()}.latte";
$this->template->setFile(is_file($file) ? $file : __DIR__ . '/templates/Error/4xx.latte');
}
}
43 changes: 43 additions & 0 deletions app/presenters/ErrorPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace App\Presenters;

use Nette;
use Nette\Application\Responses;
use Nette\Http;
use Tracy\ILogger;


final class ErrorPresenter implements Nette\Application\IPresenter
{
use Nette\SmartObject;

/** @var ILogger */
private $logger;


public function __construct(ILogger $logger)
{
$this->logger = $logger;
}


public function run(Nette\Application\Request $request): Nette\Application\IResponse
{
$exception = $request->getParameter('exception');

if ($exception instanceof Nette\Application\BadRequestException) {
[$module, , $sep] = Nette\Application\Helpers::splitName($request->getPresenterName());
return new Responses\ForwardResponse($request->setPresenterName($module . $sep . 'Error4xx'));
}

$this->logger->log($exception, ILogger::EXCEPTION);
return new Responses\CallbackResponse(function (Http\IRequest $httpRequest, Http\IResponse $httpResponse): void {
if (preg_match('#^text/html(?:;|$)#', $httpResponse->getHeader('Content-Type'))) {
require __DIR__ . '/templates/Error/500.phtml';
}
});
}
}
29 changes: 29 additions & 0 deletions app/presenters/HomepagePresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Presenters;

use Nette;


final class HomepagePresenter extends Nette\Application\UI\Presenter
{
/** @var Nette\Database\Context */
private $database;


public function __construct(Nette\Database\Context $database)
{
$this->database = $database;
}


public function renderDefault(int $page = 1): void
{
$this->template->page = $page;
$this->template->posts = $this->database->table('posts')
->order('created_at DESC')
->page($page, 5);
}
}
122 changes: 122 additions & 0 deletions app/presenters/PostPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

declare(strict_types=1);

namespace App\Presenters;

use Nette;
use Nette\Application\UI\Form;


final class PostPresenter extends Nette\Application\UI\Presenter
{
/** @var Nette\Database\Context */
private $database;


public function __construct(Nette\Database\Context $database)
{
$this->database = $database;
}


public function renderShow(int $postId): void
{
$post = $this->database->table('posts')->get($postId);
if (!$post) {
$this->error('Post not found');
}

$this->template->post = $post;
$this->template->comments = $post->related('comment')->order('created_at');
}


protected function createComponentCommentForm(): Form
{
$form = new Form;
$form->addText('name', 'Your name:')
->setRequired();

$form->addEmail('email', 'Email:');

$form->addTextArea('content', 'Comment:')
->setRequired();

$form->addSubmit('send', 'Publish comment');
$form->onSuccess[] = [$this, 'commentFormSucceeded'];

return $form;
}


public function commentFormSucceeded(Form $form, \stdClass $values): void
{
$this->database->table('comments')->insert([
'post_id' => $this->getParameter('postId'),
'name' => $values->name,
'email' => $values->email,
'content' => $values->content,
]);

$this->flashMessage('Thank you for your comment', 'success');
$this->redirect('this');
}


public function actionCreate(): void
{
if (!$this->getUser()->isLoggedIn()) {
$this->redirect('Sign:in');
}
}


public function actionEdit(int $postId): void
{
if (!$this->getUser()->isLoggedIn()) {
$this->redirect('Sign:in');
}

$post = $this->database->table('posts')->get($postId);
if (!$post) {
$this->error('Post not found');
}
$this['postForm']->setDefaults($post->toArray());
}


protected function createComponentPostForm(): Form
{
if (!$this->getUser()->isLoggedIn()) {
$this->error('You need to log in to create or edit posts');
}

$form = new Form;
$form->addText('title', 'Title:')
->setRequired();
$form->addTextArea('content', 'Content:')
->setRequired();

$form->addSubmit('send', 'Save and publish');
$form->onSuccess[] = [$this, 'postFormSucceeded'];

return $form;
}


public function postFormSucceeded(Form $form, \stdClass $values): void
{
$postId = $this->getParameter('postId');

if ($postId) {
$post = $this->database->table('posts')->get($postId);
$post->update($values);
} else {
$post = $this->database->table('posts')->insert($values);
}

$this->flashMessage('Post was published', 'success');
$this->redirect('show', $post->id);
}
}
51 changes: 51 additions & 0 deletions app/presenters/SignPresenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace App\Presenters;

use Nette;
use Nette\Application\UI\Form;


final class SignPresenter extends Nette\Application\UI\Presenter
{
/**
* Sign-in form factory.
*/
protected function createComponentSignInForm(): Form
{
$form = new Form;
$form->addText('username', 'Username:')
->setRequired('Please enter your username.');

$form->addPassword('password', 'Password:')
->setRequired('Please enter your password.');

$form->addSubmit('send', 'Sign in');

// call method signInFormSucceeded() on success
$form->onSuccess[] = [$this, 'signInFormSucceeded'];
return $form;
}


public function signInFormSucceeded(Form $form, \stdClass $values): void
{
try {
$this->getUser()->login($values->username, $values->password);
$this->redirect('Homepage:');

} catch (Nette\Security\AuthenticationException $e) {
$form->addError('Incorrect username or password.');
}
}


public function actionOut(): void
{
$this->getUser()->logout();
$this->flashMessage('You have been signed out.');
$this->redirect('Homepage:');
}
}
33 changes: 33 additions & 0 deletions app/presenters/templates/@layout.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">

<title>{ifset title}{include title|stripHtml} | {/ifset}Nette Framework Micro-blog</title>

<link rel="stylesheet" href="{$basePath}/css/style.css">
</head>

<body>
<ul class="navig">
<li><a n:href="Homepage:">Homepage</a></li>
{block navig}{/block}
{if $user->loggedIn}
<li><a n:href="Sign:out">Sign out</a></li>
{else}
<li><a n:href="Sign:in">Sign in</a></li>
{/if}
</ul>

<div n:foreach="$flashes as $flash" n:class="flash, $flash->type">{$flash->message}</div>

{include content}

<p class="footer">This is a <a href="http://doc.nette.org/quickstart">Nette Framework Quick Start</a>.</p>

{block scripts}
<script src="https://nette.github.io/resources/js/3/netteForms.min.js"></script>
{/block}
</body>
</html>
7 changes: 7 additions & 0 deletions app/presenters/templates/Error/403.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{block content}
<h1 n:block=title>Access Denied</h1>

<p>You do not have permission to view this page. Please try contact the web
site administrator if you believe you should be able to view this page.</p>

<p><small>error 403</small></p>
8 changes: 8 additions & 0 deletions app/presenters/templates/Error/404.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{block content}
<h1 n:block=title>Page Not Found</h1>

<p>The page you requested could not be found. It is possible that the address is
incorrect, or that the page no longer exists. Please use a search engine to find
what you are looking for.</p>

<p><small>error 404</small></p>
6 changes: 6 additions & 0 deletions app/presenters/templates/Error/405.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{block content}
<h1 n:block=title>Method Not Allowed</h1>

<p>The requested method is not allowed for the URL.</p>

<p><small>error 405</small></p>
6 changes: 6 additions & 0 deletions app/presenters/templates/Error/410.latte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{block content}
<h1 n:block=title>Page Not Found</h1>

<p>The page you requested has been taken off the site. We apologize for the inconvenience.</p>

<p><small>error 410</small></p>
Loading