If you followed along from our previous tutorial, you learned that we can use GET parameters to set variables for our routes. In this tutorial, we are going to take laravel's capabilites a littler further with controllers. Using a controller will make our project more neat and more manageable. From our previous tutorial, our route code looks like this:
routes/web.php:
Route::get('/page/{PageId}', function ($PageId) {
$pages = [
'contact' => 'This is the Contact us pages',
'about' => 'This is the About us pages'
];
// SHOW 404 PAGE:
if(! array_key_exists($PageId, $pages)){
abort(404, 'Page is not found');
}
Return View('pages', [
'pages' => $pages[$PageId]
]);
});
You are going to like using controllers better than what you've learned so far. To start, lets get rid of our code in routes/web.php and replace it with the following:
Route::get('/page/{PageId}', 'PagesController@show');
Now that we have defined our controller in routes/web.php we can create our new PagesController with the following file:
app/Http/Controllers/PagesController.php
<?php
namespace App\Http\Controllers;
/**
*
*/
class PagesController
{
public function show()
{
return 'hello';
}
}
?>
Try it: http://example.com/page/contact
You shoud see a 'hello' message.
With the above example, you are returing a 'hello' message. In the real world, we would want to point the GET paramater to a page, we can use Laravel's route feature and integrate it with the new controller we just created. To do that we would change our code to the following:
app/Http/Controllers/PagesController.php
<?php
namespace App\Http\Controllers;
class PagesController
{
public function show($PageId)
{
$pages = [
'contact' => 'This is the Contact us pages',
'about' => 'This is the About us pages'
];
// SHOW 404 PAGE:
if(! array_key_exists($PageId, $pages)){
abort(404, 'Page is not found');
}
Return View('pages', [
'pages' => $pages[$PageId]
]);
}
}
?>
resources/views/Pages.blade.php
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{{$pages}}</title>
</head>
<body>
<a href="http://myapp.test/?pages=contactss">http://myapp.test/?pages=contactss</a>
<h1><?= $pages; ?> PHP Unescaped</h1>
<h1><?= htmlspecialchars($pages, ENT_QUOTES); ?> PHP htmlspecialchars</h1>
<h1>{{$pages}} - Blade Escaped</h1>
<h1>{!! $pages !!} - Blade Unescaped</h1>
<hr>
<p><a href="http://myapp.test/page/contact">http://myapp.test/page/contact: </a>{{ $pages }}</p>
<p><a href="http://myapp.test/page/notInArray">http://myapp.test/page/notInArray: </a>{{ $pages }}</p>
</body>
</html>
Try it: http://example.com/page/contact
Now, let me show you a faster way to create a controller. Using the command terminal, you can easily create a controller very fast and convinient. To create a new conotroller, follow these command:
Change Directory to the project, in our example, we are in myAPP project, so I will go to that directory:
NOTE: If you are using vagrant with homestead, be sure to send this command in your SSH virtualbox. (> vagrant ssh)
$ cd /home/vagrant/code/myAPP
We are going to use artisan to create our controller. If you are not familiar with artisan, you can run this command to see what options you have. Put close attention to the make section, it has very helpful command,
$ php artisan
OUTPUT:
make
make:channel Create a new channel class
make:command Create a new Artisan command
make:controller Create a new controller class
make:event Create a new event class
make:exception Create a new custom exception class
make:factory Create a new model factory
make:job Create a new job class
make:listener Create a new event listener class
make:mail Create a new email class
make:middleware Create a new middleware class
make:migration Create a new migration file
make:model Create a new Eloquent model class
make:notification Create a new notification class
make:observer Create a new observer class
make:policy Create a new policy class
make:provider Create a new service provider class
make:request Create a new form request class
make:resource Create a new resource
make:rule Create a new validation rule
make:seeder Create a new seeder class
make:test Create a new test class
Notice one of the make is controller, that's what we are going to use. Let's create our PagesController with the following command:
$ php artisan make:controller PagesController
You will get a message: "Controller created successfully." You can also confirm by going to the app/Http/Controllers/ and you will see a file called PagesController.php the will look like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
//
}
Let's add our code and it should look like this:
app/Http/Controllers/PagesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($PageId)
{
$pages = [
'contact' => 'This is the Contact us pages',
'about' => 'This is the About us pages'
];
// SHOW 404 PAGE:
if(! array_key_exists($PageId, $pages)){
abort(404, 'Page is not found');
}
Return View('pages', [
'pages' => $pages[$PageId]
]);
}
}
Try it: http://example.com/page/contact
Note, I am using example.com - be sure to change it to whatever you have set the hostname in your Homestead.yaml file. The defaul is for homestead: homestead.test
Resource: https://laracasts.com/series/laravel-6-from-scratch/episodes/7
Hope that helps.