CodeIgniter Forums
Redirect when no parameter passed - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Redirect when no parameter passed (/showthread.php?tid=76412)



Redirect when no parameter passed - bomi - 05-10-2020

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('/');
    



RE: Redirect when no parameter passed - jreklund - 05-11-2020

Can you show us how you get the variable $id?


RE: Redirect when no parameter passed - bomi - 05-11-2020

(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);



RE: Redirect when no parameter passed - jreklund - 05-11-2020

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


RE: Redirect when no parameter passed - bomi - 05-11-2020

(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);
  



RE: Redirect when no parameter passed - jreklund - 05-11-2020

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.


RE: Redirect when no parameter passed - bomi - 05-11-2020

(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]


RE: Redirect when no parameter passed - jreklund - 05-11-2020

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


RE: Redirect when no parameter passed - bomi - 05-12-2020

(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