• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redirect when no parameter passed

#1
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)) {
      /*echo 'aaaaaa';
      die();*/
      return redirect()->to('/');
    
Reply

#2
Can you show us how you get the variable $id?
Reply

#3
(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');
setTimeout(() => window.location.href = link, 1000);
Reply

#4
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'] ?>".
Reply

#5
(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.

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'] ?>".

I fill with foreach loop it comes from database, delete button in the table. For example;

PHP Code:
<?php foreach ($portfolios as $portfolio): ?>
                <tr>
                  <td class="text-center"><?= $portfolio['project_name'?></td>
                  <td class="text-center"><?= word_limiter($portfolio['project_desc'], 5?></td>

                  <td class="text-center">
                    <button class="btn btn-danger btn-remove" targetUrl="deleteportfolio/<?= $portfolio['id'?>">Delete</button>
                  </td>
                </tr>
<?php endforeach; ?>

portfolios comes from

PHP Code:
public function portfolios()
  {
    $data = [];
    $model = new PortfolioModel();
    $data['portfolios'] = $model->findAll();
    echo view('testview'$data);
  
Reply

#6
No, so sorry. This $id.

PHP Code:
if (is_null($id) || empty($id)) {
      
/*echo 'aaaaaa';
      die();*/
      
return redirect()->to('/');
    } 

Do you have setAutoRoute() set to true and false in routing?

PS. You should look into using esc() as you are vulnerable to XSS.
Reply

#7
(05-11-2020, 01:16 PM)jreklund Wrote: No, so sorry. This $id.

PHP Code:
if (is_null($id) || empty($id)) {
      /*echo 'aaaaaa';
      die();*/
      return redirect()->to('/');
    

Do you have setAutoRoute() set to true and false in routing?

PS. You should look into using esc() as you are vulnerable to XSS.
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]
Reply

#8
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
Reply

#9
(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 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

What an easy thing... Thank you so much
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.