RouterFactory 404 Not Found

Seifeak
Člen | 6
+
0
-

Zdravím, jsem začátačník. Na všechno se snažím přijít sám, ale s tímto jsem už bezradnej.

V RouterFactory mám nastavenou route následovně:

<?php

declare(strict_types=1);

namespace App\Core;

use Nette;
use Nette\Application\Routers\RouteList;


final class RouterFactory
{
	use Nette\StaticClass;

	public static function createRouter(): RouteList
	{
		$router = new RouteList;
        $router->addRoute('<presenter>/<action>[/<id>]', 'Home:default');
        $router->addRoute('add-user', 'Database:addUser');
        $router->addRoute('add-post', 'Database:addPost');

		return $router;
	}
}

Tímto chci, aby se mi v Presenteru v cestě app/UI/Database/DatabasePresenter.php při načtení URL /add-user vytvořily záznamy do databáze přes Doctrine. Presenter vypadá následovně:

<?php
namespace App\UI\Database;

use App\Entity\Post;
use App\Entity\User;
use Doctrine\ORM\EntityManager;
use JetBrains\PhpStorm\NoReturn;
use Nette;
use Nette\Application\UI\Presenter;

class DatabasePresenter extends Presenter{
    public EntityManager $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function actionAddUser(): void
    {
        $user = new User();
        $user->setUsername("Tomáš");
        $user->setEmail("masinkatomas@gmail.com");

        $this->em->persist($user);
        $this->em->flush();

        $this->sendResponse(new Nette\Application\Responses\TextResponse('User added.'));
    }

    public function actionAddPost(): void
    {
        $user = $this->em->getRepository(User::class)->find(1);

        if (!$user) {
            $this->sendResponse(new Nette\Application\Responses\TextResponse('User not found.'));
        }

        $post = new Post();
        $post->setTitle('My First Post');
        $post->setDescription('This is the content of the post.');
        $post->setCreatedAt(new \DateTime());
        $post->setUser($user);

        $this->em->persist($post);
        $this->em->flush();

        $this->sendResponse(new Nette\Application\Responses\TextResponse('Post added.'));
    }
}

Tady je common.neon:

parameters:


application:
	errorPresenter:
		4xx: Error:Error4xx
		5xx: Error:Error5xx
	mapping:
	    App\UI\*\**Presenter


database:
	dsn: 'sqlite::memory:'
	user:
	password:


latte:
	strictTypes: yes
	strictParsing: yes
	extensions:
		- App\UI\Accessory\LatteExtension



di:
	export:
		parameters: no
		tags: no

extensions:
    console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)

includes:
    - doctrine.neon

services.neon:

services:
 - App\Core\RouterFactory::createRouter
 - Doctrine\ORM\EntityManager

search:
    - in: %appDir%
      classes:
          - *Factory
          - *Facade

Na localhostu to neháže žádný chyby, dokonce Tracy v Router ukazuje masky add-user a add-post (presenter = Database, action = addUser).
Ale když vlezu na localhost/add-user, tak mi to hodí chybu 404 Not Found.
Nenapadá vás něco, čeho jsem si mohl nevšimnout? Třeba chybný config?

Marek Bartoš
Nette Blogger | 1249
+
+1
-

Routy se řadí od specifických po obecné, takže <presenter>/<action>[/<id>] si dej nakonec.
Též může napovědět, zda je 404 z Apache nebo z Nette

nightfish
Člen | 509
+
+1
-

@Seifeak K tomu, co napsal Marek, jen doplním odkaz do dokumentace, kde je to popsané.

Seifeak
Člen | 6
+
0
-

Opravil jsem to, jak jste navrhli, ale stále to samé.
Chybu to hlási s tímto popiskem „nginx/1.18.0 (Ubuntu)“

Tady je je konfigurace nginx:

server {
        listen 81;
        listen [::]:81;

        root /var/www/learning/www;

        index index.php index.html index.htm index.nginx-debian.html;

        server_name learning.com;

        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }

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

Editoval Seifeak (22. 6. 19:46)

nightfish
Člen | 509
+
+2
-

@Seifeak V konfiguraci Nginxu je pro fungování „pěkných URL“ potřeba mít v konfiguraci něco v duchu:

location / {
	try_files $uri $uri/ /index.php$is_args$args;  # $is_args$args IS IMPORTANT!
}

Převzato z https://doc.nette.org/…ubleshooting#…
Efektivně to říká, že když Nginx narazí na URL, kterou to nezná (např. /add-user), tak to má řízení předat do index.php, kde už si to chytí Nette router a požadavek v pořádku odbaví. Zatímco tvá konfigurace =404 říká nic dalšího nezkoušej a vyhoď chybu 404.

Editoval nightfish (22. 6. 20:47)

Seifeak
Člen | 6
+
0
-

Díky, to bylo ono. Když jsem to konfiguroval tak jsem vzal default šablonu a nastavil to tak ať to jede. Doposud jsem dělelal na Apache ve XAMPP, kde stačilo hodit složku do htdocs a všechno šlapalo. Nginx je pro mě nový. Děkuji za pomoc.

nightfish napsal(a):

@Seifeak V konfiguraci Nginxu je pro fungování „pěkných URL“ potřeba mít v konfiguraci něco v duchu:

location / {
	try_files $uri $uri/ /index.php$is_args$args;  # $is_args$args IS IMPORTANT!
}

Převzato z https://doc.nette.org/…ubleshooting#…
Efektivně to říká, že když Nginx narazí na URL, kterou to nezná (např. /add-user), tak to má řízení předat do index.php, kde už si to chytí Nette router a požadavek v pořádku odbaví. Zatímco tvá konfigurace =404 říká nic dalšího nezkoušej a vyhoď chybu 404.

Marek Bartoš
Nette Blogger | 1249
+
0
-

Pro Apache to funguje prakticky stejně, ale nette/web-project má ve složce www soubor .htaccess, který tohle nastavuje za tebe