stefanp's avatar

route wildcard

Hi Guys,

I see the PHP Practitioner series and after I saw the Make a Router episode, I wanted to make an exercise that sounds as follows:

In index.php page see all tasks from a mysql database in a table. The table have the following columns: id, todo, name, completed, done, delete. When you click on done and/or delete links that corresponds with the id's from the table, you either delete the entry or mark it as completed.

Under the table I have a form where I can add extra todos in the database.

Well, I was able to insert new data into mysql and to get that data from mysql into the table but do not know how to set up the route wildcard in order to modify or delete entries from the table(and mysql).

in index.view.php i used

  <?php foreach ($tasks as $task) : ?>
                   <tr>
                       <?php if($task->completed) : ?>
                       <td><?= $task->id; ?></td><td><?= $task->todo; ?></td><td><?= $task->whom; ?></td><td>ok</td><td>-</td><td><a href="?delete_task=<?= $task->id ?>">delete</a></td>
                   <?php else : ?>
                       <td><?= $task->id; ?></td><td><?= $task->todo; ?></td><td><?= $task->whom; ?></td><td><?= $task->completed ?></td><td><a href="?mark_as_completed=<?= $task->id; ?>">bifeaza</a></td><td><a href="?delete_task=<?= $task->id ?>">delete</a></td>
                   <?php endif; ?>
                   </tr>
               <?php endforeach; ?>

and the script expects that in routes.php to have a ?delete_task=... and ?mark_as_completed=... paths

If I enter by hand '?delete_task=5' => 'controllers/indexController.php' in routes.php

and in indexController.php besides others I have:

if($_GET['delete_task'])
{
    $id = intval($_GET['delete_task']);
    $delTask = $app['database']->deleteTask('todos', $id);
    var_dump($delTask);
}

I can delete that todo with the id of 5

How can I define the route in order to be able to delete or modify any id from the table or what is a best practice in this case.

Thanks!

0 likes
4 replies
EventFellows's avatar

As you seem to be on a learning exercise I suggest you do the following

  1. create a resourceful route: https://laravel.com/docs/5.4/controllers#resource-controllers

  2. run php artisan route:list to see which routes have been created

You will get a list like this:

POST      | tasks             | tasks.store
GET|HEAD  | tasks             | tasks.index
GET|HEAD  | tasks/create      | tasks.create
DELETE    | tasks/{task}      | tasks.destroy
PUT|PATCH | tasks/{task}      | tasks.update
GET|HEAD  | tasks/{task}      | tasks.show
GET|HEAD  | tasks/{task}/edit | tasks.edit

So to delete a task you would send a DESTROY request to the route tasks/{task}

This is what is probably called very common setup on what request types you run on what kind of route structure.

To achieve this your button would need to be a FORM, not only a link like in your setup (a link will send a GET request)

Of course you can also creaste a setup where the GET call triggers the delete action but that is not standard.

Tip: You probably should consider to use blade syntax instead of php in your view. There are @foreach, @if and so on that clean up your code dramatically.

stefanp's avatar

I tried to start with Laravel but because I am at the very beginning and I do not have the basic php I decided to start with vanilla php and after that I will move on Laravel.

stefanp's avatar

I realised that I made a mistake the index.view.php actually is

    <?php foreach ($tasks as $task) : ?>
                    <tr>
                        <?php if($task->completed) : ?>
                        <td><?= $task->id; ?></td><td><?= $task->todo; ?></td><td><?= $task->whom; ?></td><td>ok</td><td>-</td><td><a href="delete_task?id=<?= $task->id ?>">delete</a></td>
                    <?php else : ?>
                        <td><?= $task->id; ?></td><td><?= $task->todo; ?></td><td><?= $task->whom; ?></td><td><?= $task->completed ?></td><td><a href="mark_as_completed?id=<?= $task->id; ?>">bifeaza</a></td><td><a href="delete_task?id=<?= $task->id ?>">delete</a></td>
                    <?php endif; ?>
                    </tr>
                <?php endforeach; ?>

and the routes.php

$router->define([
    '' => 'controllers/indexController.php',
    'delete_task' => 'controllers/indexController.php'
]);

now, when I want to delete a row from the table I have the following error:

Not Found

The requested URL /delete_task was not found on this server.

EventFellows's avatar

Ok, my suggestion what clearly assuming you are on Laravel...

If I might suggest that: I would learn PHP while using Laravel. The setup is super-simple and making good progress will be quiete a bit easier.

Please or to participate in this conversation.