{"id":168,"date":"2024-05-27T00:17:32","date_gmt":"2024-05-27T06:17:32","guid":{"rendered":"https:\/\/www.7softinteractive.net\/?p=168"},"modified":"2024-05-29T12:07:11","modified_gmt":"2024-05-29T18:07:11","slug":"php-slim-with-twig-sample","status":"publish","type":"post","link":"https:\/\/www.7softinteractive.net\/?p=168","title":{"rendered":"Php-Slim with Twig Sample"},"content":{"rendered":"\n<p><strong>Title: Building a Simple Web Application with SlimPHP and Twig on Fedora Linux<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Introduction<\/strong><\/p>\n\n\n\n<p>SlimPHP is a lightweight PHP framework that makes it easy to build web applications. Twig is a flexible, fast, and secure template engine for PHP. Together, they provide a powerful and efficient way to develop web applications. In this guide, we&#8217;ll walk you through setting up a SlimPHP application with Twig templating on Fedora Linux.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Step 1: Install PHP and Composer<\/strong><\/p>\n\n\n\n<p>First, ensure you have PHP and Composer installed on your Fedora system. Open a terminal and run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dnf install -y php php-cli php-common php-mbstring php-xml\nsudo dnf install -y composer<\/code><\/pre>\n\n\n\n<p>Verify the installations:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php --version\ncomposer --version<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Step 2: Create a New SlimPHP Project<\/strong><\/p>\n\n\n\n<p>Create a new directory for your SlimPHP project and navigate into it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir my-slim-app\ncd my-slim-app<\/code><\/pre>\n\n\n\n<p>Use Composer to create a new SlimPHP project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require slim\/slim \"^4.0\"\ncomposer require slim\/psr7\ncomposer require nyholm\/psr7\ncomposer require slim\/twig-view<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Step 3: Set Up the Project Structure<\/strong><\/p>\n\n\n\n<p>Create the following directory structure within your project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my-slim-app\/\n\u251c\u2500\u2500 public\/\n\u2502   \u2514\u2500\u2500 index.php\n\u251c\u2500\u2500 templates\/\n\u2502   \u2514\u2500\u2500 home.twig\n\u2514\u2500\u2500 src\/\n    \u2514\u2500\u2500 dependencies.php<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Step 4: Configure SlimPHP with Twig<\/strong><\/p>\n\n\n\n<p>Edit the <code>index.php<\/code> file in the <code>public<\/code> directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nrequire __DIR__ . '\/..\/vendor\/autoload.php';\n\nuse Psr\\Http\\Message\\ResponseInterface as Response;\nuse Psr\\Http\\Message\\ServerRequestInterface as Request;\nuse Slim\\Factory\\AppFactory;\nuse Slim\\Views\\Twig;\nuse Slim\\Views\\TwigMiddleware;\n\n$app = AppFactory::create();\n\n$twig = Twig::create('..\/templates', &#91;'cache' =&gt; false]);\n$app-&gt;add(TwigMiddleware::createFromContainer($app, Twig::class));\n\n$app-&gt;get('\/', function (Request $request, Response $response, $args) {\n    $view = Twig::fromRequest($request);\n    return $view-&gt;render($response, 'home.twig');\n});\n\n$app-&gt;run();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Step 5: Create a Twig Template<\/strong><\/p>\n\n\n\n<p>Create a <code>home.twig<\/code> file in the <code>templates<\/code> directory with the following content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;title&gt;My Slim App&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;Welcome to My SlimPHP Application!&lt;\/h1&gt;\n    &lt;p&gt;This is a simple web application using SlimPHP and Twig.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Step 6: Serve Your Application<\/strong><\/p>\n\n\n\n<p>To serve your SlimPHP application, use the built-in PHP server. Navigate to your project directory and run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php -S localhost:8080 -t public<\/code><\/pre>\n\n\n\n<p>Open your web browser and visit <code>http:\/\/localhost:8080<\/code>. You should see your Twig template rendered by SlimPHP.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Step 7: Add More Routes and Templates<\/strong><\/p>\n\n\n\n<p>You can add more routes and templates to expand your application. For example, create an <code>about.twig<\/code> template in the <code>templates<\/code> directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"en\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"&gt;\n    &lt;title&gt;About Us&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;About Us&lt;\/h1&gt;\n    &lt;p&gt;This is the about page.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p>Then, update <code>index.php<\/code> to include a new route for the &#8220;About&#8221; page:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nrequire __DIR__ . '\/..\/vendor\/autoload.php';\n\nuse Psr\\Http\\Message\\ResponseInterface as Response;\nuse Psr\\Http\\Message\\ServerRequestInterface as Request;\nuse Slim\\Factory\\AppFactory;\nuse Slim\\Views\\Twig;\nuse Slim\\Views\\TwigMiddleware;\n\n$app = AppFactory::create();\n\n$twig = Twig::create('..\/templates', &#91;'cache' =&gt; false]);\n$app-&gt;add(TwigMiddleware::createFromContainer($app, Twig::class));\n\n$app-&gt;get('\/', function (Request $request, Response $response, $args) {\n    $view = Twig::fromRequest($request);\n    return $view-&gt;render($response, 'home.twig');\n});\n\n$app-&gt;get('\/about', function (Request $request, Response $response, $args) {\n    $view = Twig::fromRequest($request);\n    return $view-&gt;render($response, 'about.twig');\n});\n\n$app-&gt;run();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>You have successfully set up a SlimPHP application with Twig templating on Fedora Linux. This setup provides a robust foundation for building scalable and maintainable web applications. Feel free to expand your application by adding more routes, templates, and functionality as needed. Happy coding!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Title: Building a Simple Web Application with SlimPHP and Twig on Fedora Linux Introduction SlimPHP is a lightweight PHP framework that makes it easy to build web applications. Twig is a flexible, fast, and secure template engine for PHP. Together, they provide a powerful and efficient way to develop web applications. In this guide, we&#8217;ll<\/p>\n","protected":false},"author":2,"featured_media":186,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-168","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development-environment"],"_links":{"self":[{"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/posts\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=168"}],"version-history":[{"count":2,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions\/195"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=\/wp\/v2\/media\/186"}],"wp:attachment":[{"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.7softinteractive.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}