Return with a 404 not found response

@param ServerRequestInterface $request
@param ResponseInterface $response
@return ResponseInterface
174730
protected function notFound(ServerRequestInterface $request, ResponseInterface $response)
    {
        $finalResponse = $response
            ->withProtocolVersion($request->getProtocolVersion())
            ->withStatus(404)
            ->withHeader('Content-Type', 'text/plain');
        
        $finalResponse->getBody()->write("Not found");
        
        return $finalResponse;
    }
316197
public function run(ServerRequestInterface $request, ResponseInterface $response)
    {
        $route = $request->getAttribute('route');
        $callback = !empty($route->fn) ? $route->fn : null;

        if (!is_callable($callback)) {
            trigger_error("'fn' property of route shoud be a callable", E_USER_NOTICE);
            return $this->notFound($request, $response);
        }

        return $callback($request, $response);
    }
964211
public function process(Request $request, RequestHandlerInterface $handler): ResponseInterface
    {
        if ($this->log) {
            $this->log->info('404 - Page not found on URL: '.$request->getUri()->getPath());
        }

        $response = SplashUtils::buildControllerResponse(
            function () use ($request) {
                return $this->pageNotFoundController->pageNotFound($request);
            }
        );

        return $response;
    }
339721
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $uri = $request->getUri();
        $request = $request->withUri($uri->withPath($this->removeBasePath($uri->getPath())));

        $response = $handler->handle($request);

        if ($this->fixLocation && $response->hasHeader('Location')) {
            $location = parse_url($response->getHeaderLine('Location'));

            if (empty($location['host']) || $location['host'] === $uri->getHost()) {
                $location['path'] = $this->addBasePath($location['path']);

                return $response->withHeader('Location', self::unParseUrl($location));
            }
        }

        return $response;
    }
1030008
public function handle(
        ServerRequestInterface $request, ResponseInterface $response
    )
    {
        $this->request = $request;
        $render = $this->request->getAttribute('render', true);
        if ($render) {
            $response = $this->render($response);
        }
        return $this->executeNext($request, $response);
    }
1319594
protected function returnResponse(ResponseInterface $response): ResponseInterface
    {
        $this->nextHandler = null;
        reset($this->handlers);
        return $response;
    }
730550
protected function getOutputResponse(
        ServerRequestInterface $request,
        ResponseInterface $response
    ) : ResponseInterface {
        ob_start();
        switch ($this->determineOutputType()) {
            case self::TYPE_JSON:
                $this->renderJSON($request, $response);
                break;
            case self::TYPE_XML:
                $this->renderXML($request, $response);
                break;
            case self::TYPE_PLAIN:
                $this->renderPlainText($request, $response);
                break;
            default:
                $this->renderHTML($request, $response);
                break;
        }

        $output = ob_get_clean();
        $body = new RequestBody();
        // write handler
        $body->write($output);
        return $response->withStatus(404)->withBody($body);
    }
508000
public function findByRequest(ServerRequestInterface $request)
    {
        $result = $this->findRouteByRequest($request, trim($request->getUri()->getPath(), '/'));

        if(!$result['route'])
            throw new RouteNotFoundException('Route for request '.$request->getMethod().' '.$request->getUri()->getPath().' does not exist');

        return $this->factory->createFinding($result['route'], $result['parameter'], $request);
    }
20440
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $errorPageConfig = Config::getInstance()->get('errorPage');
        $controllerClass = '\\app\\Controllers\\' . $errorPageConfig[0];
        $route = new Route('404', $errorPageConfig[0], $errorPageConfig[1]);
        $controller = new $controllerClass($route, $request);
        $response = new Response;
        ob_start();
        $controller->execute();
        $responseController = ob_get_clean();
        $response->getBody()->write($responseController);
        $response = $response->withStatus(404);
        $logger = new Logger();
        $logger->info('Error 404, NotFoundMiddleware');
        return $response;
    }
253648
public function handle(ServerRequestInterface $request):ResponseInterface {
		$path = $request->getUri()->getPath();
// TODO: Abstract response type needed.
		$response = new PageResponse();

		try {
			$templateDirectory = implode(DIRECTORY_SEPARATOR, [
				$this->router->getBaseViewLogicPath(),
				"_component",
			]);
			$viewAssembly = $this->router->getViewAssembly($path);
			$view = $this->getView(
				$response->getBody(),
				(string)$viewAssembly,
				$templateDirectory,
				$path,
				$request->getHeaderLine("accept")
			);
		}
		catch(BasenameNotFoundException $exception) {
// TODO: Handle view not found.
			die("The requested view is not found!!!");
		}

		LogicFactory::setView($view);
		$baseLogicDirectory = $this->router->getBaseViewLogicPath();

		$logicAssembly = $this->router->getLogicAssembly($path);

		$logicObjects = $this->createLogicObjects(
			$logicAssembly,
			$baseLogicDirectory,
			$request->getUri()
		);

		$this->dispatchLogicObjects($logicObjects);
		$this->injectCsrf($view);
		$view->stream();

		return $response;
	}
453643
public function primaryKeyNotFound(
        EntityRequestInterface $entityRequest,
        ServerRequestInterface $request,
        ResponseInterface $response
    ): ResponseInterface {
        $model = $entityRequest->getTableMap()->getPhpName();

        return $this->render(
            $request,
            $response,
            "Primary Key Not Found in request",
            404,
            "Primary key for {$model} not found in request"
        );
    }
862809
public function entityNotFound(
        EntityRequestInterface $entityRequest,
        ServerRequestInterface $request,
        ResponseInterface $response
    ): ResponseInterface {
        $model = $entityRequest->getTableMap()->getPhpName();
        $pk    = $entityRequest->getPrimaryKey();

        return $this->render(
            $request,
            $response,
            "Entity Not Found",
            404,
            "{$model} #{$pk} not found"
        );
    }
970046
public function run(
        ServerRequestInterface $request,
        ResponseInterface $response
    )
    {

        if ($this->running)
            throw new \RuntimeException(
                "Failed to run middleware queue: Queue is already running"
            );

        $this->running = true;
        $this->save();

        $next = function (
            ServerRequestInterface $request,
            ResponseInterface $response
        ) use (&$next) {

            if (count($this->middleware) > 0) {

                $middleware = array_shift($this->middleware);
                $response = call_user_func($middleware, $request, $response, $next);

                if (!($response instanceof ResponseInterface))
                    throw new \RuntimeException(
                        "Failed to run middleware: Middleware didn't return ".
                        "a valid ".ResponseInterface::class." object"
                    );
            }

            $this->running = false;
            $this->restore();
            return $response;
        };

        return $next($request, $response);
    }
958907
public function handle(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $type = 'psr-7';
        $this->cors->setRequest($type, $request);
        $this->cors->setResponse($type, $response);
        $this->cors->handle();

        return $this->cors->getResponse();
    }
198673
public function dispatch(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
        {
            reset($this->middleware);
            $this->response = $response;
            return $this->handle($request);
        }
1163527
public function get( ServerRequestInterface $request, ResponseInterface $response )
	{
		$view = $this->getView();

		try
		{
			$cntl = \Aimeos\Controller\Frontend::create( $this->getContext(), 'subscription' );

			if( ( $id = $view->param( 'id' ) ) != '' )
			{
				$view->items = $cntl->get( $id );
				$view->total = 1;
			}
			else
			{
				$total = 0;
				$items = $cntl->slice( $view->param( 'page/offset', 0), $view->param( 'page/limit', 25 ) )
					->sort( $view->param( 'sort' ) )->parse( $view->param( 'filter', [] ) )->search( $total );

				$view->items = $items;
				$view->total = $total;
			}

			$status = 200;
		}
		catch( \Aimeos\Controller\Frontend\Exception $e )
		{
			$status = 403;
			$view->errors = $this->getErrorDetails( $e, 'controller/frontend' );
		}
		catch( \Aimeos\MShop\Exception $e )
		{
			$status = 404;
			$view->errors = $this->getErrorDetails( $e, 'mshop' );
		}
		catch( \Exception $e )
		{
			$status = 500;
			$view->errors = $this->getErrorDetails( $e );
		}

		return $this->render( $response, $view, $status );
	}
624894
public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $input = $this->input($request);

        $payload = $this->domain->payload($input);

        return $this->responder->response($request, $payload);
    }