{"id":12,"date":"2024-11-12T20:18:17","date_gmt":"2024-11-12T20:18:17","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/sbrady\/?p=12"},"modified":"2024-11-12T20:19:11","modified_gmt":"2024-11-12T20:19:11","slug":"learning-laravel","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/sbrady\/2024\/11\/12\/learning-laravel\/","title":{"rendered":"Learning Laravel"},"content":{"rendered":"\n<p>A lot has happened since the last post on here. Mainly that the senior capstone project team that I am a part of to create a Vulnerable Web Application has decided that the best way to move forward with creating the application is through using a web framework called Laravel. This decision came off of the fact that a vulnerable web application we were going to use as a base was in PHP, and found that to make the development experience better we could switch to Laravel to do our web application in. I\u2019m really excited to develop the web application but one of the blockers that I am having with the project is learning Laravel itself, so I wanted to take this blog post to try and teach what I\u2019ve learned about Laravel so far to you so that I may reinforce the learning that I\u2019ve done and teach you all something&#8217;s that might be interesting to you! Do note, this won\u2019t be a full coverage of everything Laravel has to offer, you can find that <a href=\"https:\/\/laravel.com\/docs\/11.x\">here<\/a>, but this will cover certain aspects of the framework that I learned about and how it pertains to my portion of work for the project.<\/p>\n\n\n\n<p>First, one of the main things that you learn with Laravel is how it operates on a Model-View-Controller (MVC) architecture. Simply speaking, there are three logical components being the:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Model<\/strong>: Responsible for representing database data and interacting with the database<\/li>\n\n\n\n<li><strong>View<\/strong>: Responsible for presenting data to the user. Laravel does this through Blade templates that let you compile HTML with dynamic elements.<\/li>\n\n\n\n<li><strong>Controller<\/strong>: Responsible for processing requests, retrieving data from Models, and passing the data to Views<\/li>\n<\/ul>\n\n\n\n<p>Once you understand how the architecture that Laravel operates on, next is how a new project is structured, as defined below. <strong>Note <\/strong>that these are not all the directories that Laravel generates for your project, but I feel that they are the most important ones:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>App\n<ul class=\"wp-block-list\">\n<li>Http\/Controllers\n<ul class=\"wp-block-list\">\n<li>PHP code files that represent the main logic for a webpage and interacts with your views and models.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Models\n<ul class=\"wp-block-list\">\n<li>Representations of your database table and serves as a layer of abstraction for interacting with that data.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Database\n<ul class=\"wp-block-list\">\n<li>Contains database migrations, model factories, and seeds.\n<ul class=\"wp-block-list\">\n<li>Can also house SQLite databases<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Public\n<ul class=\"wp-block-list\">\n<li>Contains the `index.php` file as the entry point to the application.<\/li>\n\n\n\n<li>Also houses assets like CSS or JavaScript.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Resources\n<ul class=\"wp-block-list\">\n<li>Where you will store your views (aka Blade template files).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Routes\n<ul class=\"wp-block-list\">\n<li>Contains all routing definitions within `web.php`.\n<ul class=\"wp-block-list\">\n<li>`web.php` contains the routes themselves.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>For me, after we set up the initial parts of the website I was tasked to making one of my webpages, being the Patient Feedback page that will house both Stored and Reflective XSS vulnerabilities. You need to create your <strong>database<\/strong>, <strong>models <\/strong>and <strong>controllers <\/strong>separately with a PHP tool called artisan, a command line tool that installs with Laravel once you created the project. Using the commands <code>php artisan make:migration create_names_table<\/code>, <code>php artisan make:model Name<\/code>, and <code>php artisan make:controller NameController<\/code> will create the database, model, and controllers respectively. For the <strong>view<\/strong>, you can create a file within the resources\/views directory with the extension <code>.blade.php<\/code> where you can write HTML with some special Blade formatting. Next, within routes\/web.php you can define the routes needed to access your webpage like this <code>Route::get('\/name', 'NameController@index')\u2192name(\u2018name.index');<\/code>, however, because we already have an authentication plugin installed almost all routes to our team&#8217;s web application will look like <code>Route::get('\/name', 'NameController@index')\u2192middleware(['auth', 'verified'])\u2192 name(\u2018name.index');<\/code> where middleware is just a mechanism for filtering incoming HTTP requests where this authentication middleware needs the user to be verified to access this page. Lastly is the database, located in <code>database\/migrations<\/code> as there you define what the table you just created in the database will look like through the premade `up` function like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"531\" height=\"340\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8096\/files\/2024\/11\/image.png\" alt=\"\" class=\"wp-image-13\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8096\/files\/2024\/11\/image.png 531w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/8096\/files\/2024\/11\/image-300x192.png 300w\" sizes=\"auto, (max-width: 531px) 100vw, 531px\" \/><\/figure>\n\n\n\n<p>Moving forward with the controller and model, however, is something that is very dependent on the implementation of the web page you are trying to create and not something I fully understand right now. The commands that were talked about earlier will create outlines for you for both a Model and Controller, but it is up to you on how to implement the logic of the web page and how it interacts with the database.<\/p>\n\n\n\n<p>Thank you for reading through, and I hope you learned something new today about how Laravel works and how to get started with developing in it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A lot has happened since the last post on here. Mainly that the senior capstone project team that I am a part of to create a Vulnerable Web Application has decided that the best way to move forward with creating the application is through using a web framework called Laravel. This decision came off of [&hellip;]<\/p>\n","protected":false},"author":14486,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/sbrady\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/sbrady\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/sbrady\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/sbrady\/wp-json\/wp\/v2\/users\/14486"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/sbrady\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":2,"href":"https:\/\/blogs.oregonstate.edu\/sbrady\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":15,"href":"https:\/\/blogs.oregonstate.edu\/sbrady\/wp-json\/wp\/v2\/posts\/12\/revisions\/15"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/sbrady\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/sbrady\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/sbrady\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}