Redirect when no parameter passed |
Hi. I wanted to redirect user if there is no parameter passed into the method. But it does not works what should I do?
My Route PHP Code: $routes->get('deleteportfolio/(:num)', 'AdminHome::deleteportfolio/$1', ['filter' => 'shouldlogged']); My Method inside of the AdminHome class PHP Code: if (is_null($id) || empty($id)) { (05-11-2020, 09:55 AM)jreklund Wrote: Can you show us how you get the variable $id? I have a custom attribute. It works but when I write manually to address bar http://baseurl/deleteportfolio it doesn't work. PHP Code: <button class="btn btn-danger btn-remove" targetUrl="deleteportfolio/<?= $portfolio['id'] ?>">Delete Project</button> Code: let link = $('[targetUrl]').attr('targetUrl');
Sorry for not specifying, but how you are assigning the variable $id. How do you populate that one.
Are you sanitizing that one? Making sure it's only an integer? So you don't are subject to XSS attacks due to "<?= $portfolio['id'] ?>". (05-11-2020, 01:03 PM)jreklund Wrote: Sorry for not specifying, but how you are assigning the variable $id. How do you populate that one. I fill with foreach loop it comes from database, delete button in the table. For example; PHP Code: <?php foreach ($portfolios as $portfolio): ?> portfolios comes from PHP Code: public function portfolios()
No, so sorry. This $id.
PHP Code: if (is_null($id) || empty($id)) { Do you have setAutoRoute() set to true and false in routing? PS. You should look into using esc() as you are vulnerable to XSS. (05-11-2020, 01:16 PM)jreklund Wrote: No, so sorry. This $id.That $id is the parameter of /deleteportfolio/$id ? And I'll look esc() function I've added video, it does not work [Video: https://www.youtube.com/watch?v=ZW8AdAjXyAY]
What setAutoRoute(true) does are that it's turning the first segment of the url into a Controller name. In this case it's looking for an controller named "Deleteportfolio", and trying to access it with the method index() as you didn't specify a second segment (the method name).
What you need to do are to create a second route: PHP Code: $routes->get('deleteportfolio', 'Homepage::index'); Or point it into deleteportfolio, if you want it to redirect. PHP Code: $routes->get('deleteportfolio', 'AdminHome::deleteportfolio'); Or redirect inside the router, now you don't need the is_null($id) check. PHP Code: $routes->addRedirect('deleteportfolio', '/'); This are something you need to be doing to all urls you are remapping from the normal example.com/controllername/method/id (05-11-2020, 10:52 PM)jreklund Wrote: What setAutoRoute(true) does are that it's turning the first segment of the url into a Controller name. In this case it's looking for an controller named "Deleteportfolio", and trying to access it with the method index() as you didn't specify a second segment (the method name). What an easy thing... Thank you so much |
Welcome Guest, Not a member yet? Register Sign In |