Slim PHP framework 3.0: 404 error when accessing .phtml template routes





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Server configuration:




  • Ubuntu 18.04

  • Apache 2.4.29

  • PostgreSQL 10.6

  • PHP 7.3 (I was using 7.2 but the system has decided to update it for me, I will rollback once I have resolved the current bugs)

  • Composer

  • Slim/Slim 3.0

  • Slim/php-view


I have been building an API with the Slim PHP framework, using gothinkster's repository as a starting point. The code has been heavily adapted, though all code relating to rendering templates remains intact. When I access the website's homepage, the index.phtml file is rendered properly, but following any hyperlinks from th homepage results in a 404 error, although the .phtml files all reside within the same directory.



The following are excerpts of my code, excluding irrelevant configuration. Although I have only displayed one endpoint, the API defines 10 endpoints, all of which are get requests with no arguments, using .phtml files which reside within the same folder.



Settings, routes and dependencies are called using require statements within the index.php file.



/[api_root]/public/index.php:



if (PHP_SAPI == 'cli-server') {

$url = parse_url($_SERVER['REQUEST_URI']);

$file = __DIR__ . $url['path'];

if (is_file($file)) {

return false;

}
}

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

session_start();

// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';

$app = new SlimApp($settings);

// Set up dependencies
require __DIR__ . '/../src/dependencies.php';

// Register middleware
require __DIR__ . '/../src/middleware.php';

// Register routes
require __DIR__ . '/../src/routes.php';

// Run app
$app->run();


The settings file returns an array containing a reference to the template path.



[api_root]/src/settings.php:



'settings' => [
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
]
]


Templates are assigned to the renderer within the dependencies.php file.



[api_root]/src/dependencies.php:



$container = $app->getContainer();

// view renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new SlimViewsPhpRenderer($settings['template_path']);
};


.phtml templates are rendered within get requests.



[api_root]/src/routes.php



$app->get('/getting-started',
function (Request $request, Response $response) {
return $this->renderer->render($response, 'getting-started.phtml');
});


The getting-started.phtml file is located within [api_root]/templates/.



The root / is defined and successfully returns the index.phtml file located within the templates directory, but when I attempt to follow any links from the homepage, I recieve a 404 error. The anchor which points to the getting-started page successfully redirects to mywebsiteurl.com/getting-started, but the template file is not rendered and the browser responds with a 404 error.



All application files are stored privately on the server. I have created symlinks from the contents of the [api_root]/public folder to my website's public_html folder.



There is obviously something wrong with my definition of the path to the /templates directory, but I can't work out how to rectify it.



EDIT:



When I created the symlinks to the public_html folder, I had forgotten to include hidden files (namely, the .htaccess file). I have since created this symlink, though now when I attempt to access the homepage I see the default apache2 page which is displayed after apache is installed. I will note here that the website resides on a paid server. Apache is configured to use virtual hosts and I have edited the hosts file on my local machine properly. As stated, without an .htaccess file I can view the homepage, but other routes are not being found. When the .htaccess file exists in the public_html folder, I cannot view the homepage or any other routes.



.htaccess:



<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::2$
RewriteCond %{HTTP_HOST} ^(www.)?mywebsiteurl.com
RewriteRule ^(.*) - [E=BASE:%1]

# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>








share

























  • A 404 error does not mean your app can't find getting-started.phtml file. This is probably related to the .htaccess file. Could you add the content of that file to your question please?

    – Nima
    Nov 24 '18 at 12:45











  • @Nima thanks for pointing that out. I've updated my answer. As I explain in the edit, I had actually forgotten to create a symlink for the .htaccess file, but since adding it I cannot even access the homepage and I'm redirected to the default apache page.

    – Joshua Flood
    Nov 25 '18 at 1:07


















0















Server configuration:




  • Ubuntu 18.04

  • Apache 2.4.29

  • PostgreSQL 10.6

  • PHP 7.3 (I was using 7.2 but the system has decided to update it for me, I will rollback once I have resolved the current bugs)

  • Composer

  • Slim/Slim 3.0

  • Slim/php-view


I have been building an API with the Slim PHP framework, using gothinkster's repository as a starting point. The code has been heavily adapted, though all code relating to rendering templates remains intact. When I access the website's homepage, the index.phtml file is rendered properly, but following any hyperlinks from th homepage results in a 404 error, although the .phtml files all reside within the same directory.



The following are excerpts of my code, excluding irrelevant configuration. Although I have only displayed one endpoint, the API defines 10 endpoints, all of which are get requests with no arguments, using .phtml files which reside within the same folder.



Settings, routes and dependencies are called using require statements within the index.php file.



/[api_root]/public/index.php:



if (PHP_SAPI == 'cli-server') {

$url = parse_url($_SERVER['REQUEST_URI']);

$file = __DIR__ . $url['path'];

if (is_file($file)) {

return false;

}
}

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

session_start();

// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';

$app = new SlimApp($settings);

// Set up dependencies
require __DIR__ . '/../src/dependencies.php';

// Register middleware
require __DIR__ . '/../src/middleware.php';

// Register routes
require __DIR__ . '/../src/routes.php';

// Run app
$app->run();


The settings file returns an array containing a reference to the template path.



[api_root]/src/settings.php:



'settings' => [
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
]
]


Templates are assigned to the renderer within the dependencies.php file.



[api_root]/src/dependencies.php:



$container = $app->getContainer();

// view renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new SlimViewsPhpRenderer($settings['template_path']);
};


.phtml templates are rendered within get requests.



[api_root]/src/routes.php



$app->get('/getting-started',
function (Request $request, Response $response) {
return $this->renderer->render($response, 'getting-started.phtml');
});


The getting-started.phtml file is located within [api_root]/templates/.



The root / is defined and successfully returns the index.phtml file located within the templates directory, but when I attempt to follow any links from the homepage, I recieve a 404 error. The anchor which points to the getting-started page successfully redirects to mywebsiteurl.com/getting-started, but the template file is not rendered and the browser responds with a 404 error.



All application files are stored privately on the server. I have created symlinks from the contents of the [api_root]/public folder to my website's public_html folder.



There is obviously something wrong with my definition of the path to the /templates directory, but I can't work out how to rectify it.



EDIT:



When I created the symlinks to the public_html folder, I had forgotten to include hidden files (namely, the .htaccess file). I have since created this symlink, though now when I attempt to access the homepage I see the default apache2 page which is displayed after apache is installed. I will note here that the website resides on a paid server. Apache is configured to use virtual hosts and I have edited the hosts file on my local machine properly. As stated, without an .htaccess file I can view the homepage, but other routes are not being found. When the .htaccess file exists in the public_html folder, I cannot view the homepage or any other routes.



.htaccess:



<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::2$
RewriteCond %{HTTP_HOST} ^(www.)?mywebsiteurl.com
RewriteRule ^(.*) - [E=BASE:%1]

# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>








share

























  • A 404 error does not mean your app can't find getting-started.phtml file. This is probably related to the .htaccess file. Could you add the content of that file to your question please?

    – Nima
    Nov 24 '18 at 12:45











  • @Nima thanks for pointing that out. I've updated my answer. As I explain in the edit, I had actually forgotten to create a symlink for the .htaccess file, but since adding it I cannot even access the homepage and I'm redirected to the default apache page.

    – Joshua Flood
    Nov 25 '18 at 1:07














0












0








0








Server configuration:




  • Ubuntu 18.04

  • Apache 2.4.29

  • PostgreSQL 10.6

  • PHP 7.3 (I was using 7.2 but the system has decided to update it for me, I will rollback once I have resolved the current bugs)

  • Composer

  • Slim/Slim 3.0

  • Slim/php-view


I have been building an API with the Slim PHP framework, using gothinkster's repository as a starting point. The code has been heavily adapted, though all code relating to rendering templates remains intact. When I access the website's homepage, the index.phtml file is rendered properly, but following any hyperlinks from th homepage results in a 404 error, although the .phtml files all reside within the same directory.



The following are excerpts of my code, excluding irrelevant configuration. Although I have only displayed one endpoint, the API defines 10 endpoints, all of which are get requests with no arguments, using .phtml files which reside within the same folder.



Settings, routes and dependencies are called using require statements within the index.php file.



/[api_root]/public/index.php:



if (PHP_SAPI == 'cli-server') {

$url = parse_url($_SERVER['REQUEST_URI']);

$file = __DIR__ . $url['path'];

if (is_file($file)) {

return false;

}
}

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

session_start();

// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';

$app = new SlimApp($settings);

// Set up dependencies
require __DIR__ . '/../src/dependencies.php';

// Register middleware
require __DIR__ . '/../src/middleware.php';

// Register routes
require __DIR__ . '/../src/routes.php';

// Run app
$app->run();


The settings file returns an array containing a reference to the template path.



[api_root]/src/settings.php:



'settings' => [
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
]
]


Templates are assigned to the renderer within the dependencies.php file.



[api_root]/src/dependencies.php:



$container = $app->getContainer();

// view renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new SlimViewsPhpRenderer($settings['template_path']);
};


.phtml templates are rendered within get requests.



[api_root]/src/routes.php



$app->get('/getting-started',
function (Request $request, Response $response) {
return $this->renderer->render($response, 'getting-started.phtml');
});


The getting-started.phtml file is located within [api_root]/templates/.



The root / is defined and successfully returns the index.phtml file located within the templates directory, but when I attempt to follow any links from the homepage, I recieve a 404 error. The anchor which points to the getting-started page successfully redirects to mywebsiteurl.com/getting-started, but the template file is not rendered and the browser responds with a 404 error.



All application files are stored privately on the server. I have created symlinks from the contents of the [api_root]/public folder to my website's public_html folder.



There is obviously something wrong with my definition of the path to the /templates directory, but I can't work out how to rectify it.



EDIT:



When I created the symlinks to the public_html folder, I had forgotten to include hidden files (namely, the .htaccess file). I have since created this symlink, though now when I attempt to access the homepage I see the default apache2 page which is displayed after apache is installed. I will note here that the website resides on a paid server. Apache is configured to use virtual hosts and I have edited the hosts file on my local machine properly. As stated, without an .htaccess file I can view the homepage, but other routes are not being found. When the .htaccess file exists in the public_html folder, I cannot view the homepage or any other routes.



.htaccess:



<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::2$
RewriteCond %{HTTP_HOST} ^(www.)?mywebsiteurl.com
RewriteRule ^(.*) - [E=BASE:%1]

# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>








share
















Server configuration:




  • Ubuntu 18.04

  • Apache 2.4.29

  • PostgreSQL 10.6

  • PHP 7.3 (I was using 7.2 but the system has decided to update it for me, I will rollback once I have resolved the current bugs)

  • Composer

  • Slim/Slim 3.0

  • Slim/php-view


I have been building an API with the Slim PHP framework, using gothinkster's repository as a starting point. The code has been heavily adapted, though all code relating to rendering templates remains intact. When I access the website's homepage, the index.phtml file is rendered properly, but following any hyperlinks from th homepage results in a 404 error, although the .phtml files all reside within the same directory.



The following are excerpts of my code, excluding irrelevant configuration. Although I have only displayed one endpoint, the API defines 10 endpoints, all of which are get requests with no arguments, using .phtml files which reside within the same folder.



Settings, routes and dependencies are called using require statements within the index.php file.



/[api_root]/public/index.php:



if (PHP_SAPI == 'cli-server') {

$url = parse_url($_SERVER['REQUEST_URI']);

$file = __DIR__ . $url['path'];

if (is_file($file)) {

return false;

}
}

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

session_start();

// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';

$app = new SlimApp($settings);

// Set up dependencies
require __DIR__ . '/../src/dependencies.php';

// Register middleware
require __DIR__ . '/../src/middleware.php';

// Register routes
require __DIR__ . '/../src/routes.php';

// Run app
$app->run();


The settings file returns an array containing a reference to the template path.



[api_root]/src/settings.php:



'settings' => [
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
]
]


Templates are assigned to the renderer within the dependencies.php file.



[api_root]/src/dependencies.php:



$container = $app->getContainer();

// view renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new SlimViewsPhpRenderer($settings['template_path']);
};


.phtml templates are rendered within get requests.



[api_root]/src/routes.php



$app->get('/getting-started',
function (Request $request, Response $response) {
return $this->renderer->render($response, 'getting-started.phtml');
});


The getting-started.phtml file is located within [api_root]/templates/.



The root / is defined and successfully returns the index.phtml file located within the templates directory, but when I attempt to follow any links from the homepage, I recieve a 404 error. The anchor which points to the getting-started page successfully redirects to mywebsiteurl.com/getting-started, but the template file is not rendered and the browser responds with a 404 error.



All application files are stored privately on the server. I have created symlinks from the contents of the [api_root]/public folder to my website's public_html folder.



There is obviously something wrong with my definition of the path to the /templates directory, but I can't work out how to rectify it.



EDIT:



When I created the symlinks to the public_html folder, I had forgotten to include hidden files (namely, the .htaccess file). I have since created this symlink, though now when I attempt to access the homepage I see the default apache2 page which is displayed after apache is installed. I will note here that the website resides on a paid server. Apache is configured to use virtual hosts and I have edited the hosts file on my local machine properly. As stated, without an .htaccess file I can view the homepage, but other routes are not being found. When the .htaccess file exists in the public_html folder, I cannot view the homepage or any other routes.



.htaccess:



<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::2$
RewriteCond %{HTTP_HOST} ^(www.)?mywebsiteurl.com
RewriteRule ^(.*) - [E=BASE:%1]

# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>






php api routing http-status-code-404 slim





share














share












share



share








edited Nov 25 '18 at 2:47







Joshua Flood

















asked Nov 24 '18 at 8:45









Joshua FloodJoshua Flood

2601317




2601317













  • A 404 error does not mean your app can't find getting-started.phtml file. This is probably related to the .htaccess file. Could you add the content of that file to your question please?

    – Nima
    Nov 24 '18 at 12:45











  • @Nima thanks for pointing that out. I've updated my answer. As I explain in the edit, I had actually forgotten to create a symlink for the .htaccess file, but since adding it I cannot even access the homepage and I'm redirected to the default apache page.

    – Joshua Flood
    Nov 25 '18 at 1:07



















  • A 404 error does not mean your app can't find getting-started.phtml file. This is probably related to the .htaccess file. Could you add the content of that file to your question please?

    – Nima
    Nov 24 '18 at 12:45











  • @Nima thanks for pointing that out. I've updated my answer. As I explain in the edit, I had actually forgotten to create a symlink for the .htaccess file, but since adding it I cannot even access the homepage and I'm redirected to the default apache page.

    – Joshua Flood
    Nov 25 '18 at 1:07

















A 404 error does not mean your app can't find getting-started.phtml file. This is probably related to the .htaccess file. Could you add the content of that file to your question please?

– Nima
Nov 24 '18 at 12:45





A 404 error does not mean your app can't find getting-started.phtml file. This is probably related to the .htaccess file. Could you add the content of that file to your question please?

– Nima
Nov 24 '18 at 12:45













@Nima thanks for pointing that out. I've updated my answer. As I explain in the edit, I had actually forgotten to create a symlink for the .htaccess file, but since adding it I cannot even access the homepage and I'm redirected to the default apache page.

– Joshua Flood
Nov 25 '18 at 1:07





@Nima thanks for pointing that out. I've updated my answer. As I explain in the edit, I had actually forgotten to create a symlink for the .htaccess file, but since adding it I cannot even access the homepage and I'm redirected to the default apache page.

– Joshua Flood
Nov 25 '18 at 1:07












1 Answer
1






active

oldest

votes


















0














The issue here was that I had not enabled mod_rewrite on the server, so my .htaccess file was not being read.





share































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The issue here was that I had not enabled mod_rewrite on the server, so my .htaccess file was not being read.





    share




























      0














      The issue here was that I had not enabled mod_rewrite on the server, so my .htaccess file was not being read.





      share


























        0












        0








        0







        The issue here was that I had not enabled mod_rewrite on the server, so my .htaccess file was not being read.





        share













        The issue here was that I had not enabled mod_rewrite on the server, so my .htaccess file was not being read.






        share











        share


        share










        answered Nov 29 '18 at 7:09









        Joshua FloodJoshua Flood

        2601317




        2601317

















            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()