CodeIgniter Forums
Problem when calling controller method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Problem when calling controller method (/showthread.php?tid=71851)

Pages: 1 2


Problem when calling controller method - rlazarotto - 10-01-2018

Hi there!

This is my first time posting here. I'm having an issue thats been haunting me for a while. I work on a project locally and, when everything is ready, I commit to a private git repository, which then goes live. The problem is that when it goes live, I start getting 404 errors everywhere, with the following output:

Quote:The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

When I run this function on my local server, the result comes as expected:

[Image: stOkbI4.png]

The same function, the same code, the same parameters. Curiously enough, not all functions do this, just some of them.

Has anyone faced this before? What can I do about it? Since I have nowhere to start, I'm lost of what can I do. As a 'workaround', I've noticed that changing the name of the function to something completely different sometimes works. For once, I had a method called 'selectData'. It wasn't working. After I changed its name to 'getSelectCache', it started to work.


RE: Problem when calling controller method - Chroma - 10-01-2018

Hi rlazarotto,

This might just be a crazy thought, but are you using the caching system to cache output?


RE: Problem when calling controller method - rlazarotto - 10-01-2018

(10-01-2018, 12:59 PM)Chroma Wrote: Hi rlazarotto,

This might just be a crazy thought, but are you using the caching system to cache output?

As far as I remember, I only use the caching system for 3 global methods. The methods that are throwing me errors aren't cached at all.


RE: Problem when calling controller method - Pertti - 10-01-2018

So you change method name inside controller, but not controller name, and it starts working?

Chrome seems to sometimes cache results, so if you had issue with something, at some point, it might have been cached on browser side, and by changing method name, you create completely new URL that won't be cached, so you can see it working.

Maybe try Chrome Incognito mode or other browsers / curl direct to see if you get working results.


RE: Problem when calling controller method - rlazarotto - 10-02-2018

(10-01-2018, 11:18 PM)Pertti Wrote: So you change method name inside controller, but not controller name, and it starts working?

Chrome seems to sometimes cache results, so if you had issue with something, at some point, it might have been cached on browser side, and by changing method name, you create completely new URL that won't be cached, so you can see it working.

Maybe try Chrome Incognito mode or other browsers / curl direct to see if you get working results.

It depends. Sometimes I have to change the name 2, 3 times before it starts working. I don't see why it might be a cache issue since the methods where new, but I gave a try running with Firefox. Same result.

I really don't know why that happens.


RE: Problem when calling controller method - Pertti - 10-02-2018

Ok, just throwing things out there that I would have come across in personal experience and would check.

Sometimes database queries get cached on DB side, it takes long time to load original request, but any future requests for same query come up super quick.

Like you say, it does not make much sense that changing method name alone makes difference.

Maybe check if you have any queries running on DB, pretty sure I had similar case only recently where first DB query would not fail straight out, but just case 500 error, and refresh on same page loaded everything. It was SQL related issue in the end.


RE: Problem when calling controller method - rlazarotto - 10-02-2018

(10-02-2018, 06:16 AM)Pertti Wrote: Ok, just throwing things out there that I would have come across in personal experience and would check.

Sometimes database queries get cached on DB side, it takes long time to load original request, but any future requests for same query come up super quick.

Like you say, it does not make much sense that changing method name alone makes difference.

Maybe check if you have any queries running on DB, pretty sure I had similar case only recently where first DB query would not fail straight out, but just case 500 error, and refresh on same page loaded everything. It was SQL related issue in the end.

I really don't think this can be a DB related issue. Chrome says it can't find the CodeIgniter controller method I'm calliing, so it doesn't even reach the API call that makes the DB query part. Firefox had the same issue.

Usually to fix this I write a new name that has nothing to do with the original. In order to still keep some relevance, I translate the method name to portguese (my native language), so I can still know what the function does without having to look at its code. Its the only way to make it work. Its like CI couldn't find the route to the function, but I use the default routing mechanism (http://server/controller/method).


RE: Problem when calling controller method - Pertti - 10-02-2018

So it comes up with CodeIgniter 404 page?

I guess without source code for controller or routes file it's hard to say.

You said it's an API - are you using CI Restserver or similar? https://github.com/chriskacerguis/codeigniter-restserver


RE: Problem when calling controller method - rlazarotto - 10-02-2018

(10-02-2018, 08:23 AM)Pertti Wrote: So it comes up with CodeIgniter 404 page?

I guess without source code for controller or routes file it's hard to say.

You said it's an API - are you using CI Restserver or similar? https://github.com/chriskacerguis/codeigniter-restserver

I believe that I forgot to tell something very important: I'm making AJAX calls to those methods. They're meant to run as form actions, but called by AJAX. Usually, I have a view with a form where user can select a few filters. Then, on the form's submit event, I send them via AJAX to a function inside the controller. This function then calls the API, and outputs the data either on a table (DataTables/JS Grid) or on a file (CSV/XLSX). I'm getting the error on the first post when I do the AJAX call. However, trying to directly open the related function on the browser produces the same result.

About the controller code, take a look. This is my routes.php file (I've left out all the comments):
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'auth';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE

This is the public method that I call to show the view (it goes on the address bar). It is inside a controller file called Reports.php. I access it by going on http://server/reports/storeLatePayment:
PHP Code:
 public function storeLatePayment() {
 
       $data = [
 
           'controller' => 'Reports',
 
           'view'       => 'storeLatePayment',
 
           'config'     => [
 
               'titleContentPage' => 'Listagem Barra I',
 
               'descriptionForm'  => 'Lista os clientes que estão com o primeiro pagamento atrasado.',
 
               'breadcrumb'       => [
 
                   'Reports',
 
                   'Listagem Barra I'
 
               ],
 
           ],
 
           'load'       => [
 
               'assets/pages/Reports/StoreLatePayment.js?t=' time(),
 
           ]
 
       ];
 
       $this->load->view('index'$data);
 
   

This is StoreLatePayment.js file:
Code:
$(document).ready(function () {
   "use strict";

   let tableB1 = $('#tableBarra1').DataTable({
       ajax: base_url + "reports/getStoresLatePaymentTableData",
       dom: "<'row'<'col-sm-12 col-md-6'l><'col-sm-12 col-md-6'f>>" +
           "<'row'<'col-sm-12'tr>><'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
       bInfo: true,
       autoWidth: true,
       bFilter: true,
       scrollX: false,
       columns: [
           {
               data: "CPF",
               render: function (data) {
                   data = data.padStart(11, '0');
                   return data.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/g, "\$1.\$2.\$3\-\$4");
               }
           },
           {data: "NOME"},
           {
               data: "Valor_Prest",
               render: function (data) {
                   return 'R$ ' + data;
               }

           },
           {
               data: "ATRASO",
               render: function (data) {
                   return '<span class="font-18 text-red"><b>' + data + '</b></span>';

               }
           },
           {
               defaultContent: "<button type='button' class='btn btn-info btn-sm waves-effect waves-light'>" +
                   "Detalhes</button>"
           }
       ],
       order: [[0, 'asc']],
       language: {
           lengthMenu: 'Mostrar _MENU_ itens por página',
           search: '<i class="fas fa-search"></i>',
           paginate: {
               previous: '<i class="fas fa-caret-left"></i>',
               next: '<i class="fas fa-caret-right"></i>'
           },
           info: 'Mostrando de _START_ até _END_ de um total de _TOTAL_ entradas.'
       }
   });
   let cpfToSearch = 0;
   tableB1.on('click', 'button', function () {
       let rowinfo = tableB1.row($(this).parents('tr')).data();
       let CPF = rowinfo.CPF;
       cpfToSearch = CPF.padStart(11, '0');
       CPF = CPF.padStart(11, '0');
       CPF = CPF.replace(/(\d{3})(\d{3})(\d{3})(\d{2})/g, "\$1.\$2.\$3\-\$4");
       $('.nome').text(rowinfo.NOME);
       $('.cpf').text(CPF);
       $('.contrato').text(rowinfo.CONTRATO);
       $('.data-compra').text(rowinfo.DATA_LANCTO);
       $('.vencimento-parcela').text(rowinfo.venctoAberto);
       $('.dias-atraso').text(rowinfo.ATRASO + ' dias');
       $('.valor-total').text('R$ ' + rowinfo.Valor);
       $('.valor-parcela').text('R$ ' + rowinfo.Valor_Prest);
       $('.parcela-atraso').text(rowinfo.NUM_PREST + ' de ' + rowinfo.TOTAL_PREST);
       $('.produto').text(rowinfo.CodProduto + ' - ' + rowinfo.Mercadoria);
       $.post(base_url + 'reports/getClientLastProposalInfo', {cpf: cpfToSearch}, function () {
       }).done(function (data) {
           data = $.parseJSON(data);

           if (data.status) {
               $('.tel-residencial').text('(' + data.data.ddd_res + ') ' + data.data.tel_res);
               $('.tel-celular').text('(' + data.data.ddd_cel + ') ' + data.data.tel_cel);
               $('.tel-comercial').text('(' + data.data.ddd_com + ') ' + data.data.tel_com);
               $('.nome-ref1').text(rowinfo.NOME_REF1);
               $('.tel-ref1').text('(' + rowinfo.DDD_REF1.trim() + ') ' + rowinfo.TEL_REF1.trim());
               $('.nome-ref2').text(rowinfo.NOME_REF2);
               $('.tel-ref2').text('(' + rowinfo.DDD_REF2.trim() + ') ' + rowinfo.TEL_REF2.trim());
               $('#infoModal').modal();
           } else {
               showToast('error', 'Erro ao obter informações de contato do cliente selecionado!');
           }
       });
   });
});
The following is the getStoresLatePaymentTableData, referenced in the JS file above. It lives in the same file as storeLatePayment  method:
PHP Code:
public function getStoresLatePaymentTableData() {
 
       $data = [
 
           'operacao'          => $this->session->userData->Operacao,
 
           'grupo'             => $this->session->userData->Grupo,
 
           'loja'              => $this->session->userData->CodLocal,
 
           'atrasoInicial'     => 5,
 
           'atrasoFinal'       => 60,
 
           'vencimentoInicial' => date('Ymd'strtotime('first day of last month')),
 
           'vencimentoFinal'   => date('Ymd'strtotime('last day of this month'))
 
       ];
 
       
        $storeLatePayment 
$this->reportslibrary->storeLatePayment($data);
 
       
        echo json_encode
($storeLatePayment);
 
   }
 
    

This is the last method mentioned on this code, getClientLastProposalInfo. I don't know if it is currently working as the getStoresLatePaymentTableData method is throwing 404 error:
PHP Code:
public function getClientLastProposalInfo() {
 
       $data = [
 
           'cpf' => $this->input->post('cpf')
 
       ];
 
       $clientInfo $this->clientslibrary->lastProposal($data);
 
       echo json_encode($clientInfo);
 
   
As you can see, they're fairly simple and nothing out of the ordinary. 

Do you guys need anything else?


RE: Problem when calling controller method - Pertti - 10-02-2018

With AJAX trailing slash could cause problems sometimes, but don't think it's that this time.

You could put die('xxx'); as first thing in your controller method.

If xxx appears when you go to URL, it means routing works, and issue must be with rest of the controller/model code. If xxx does not appear it's either routing or __construct method where you need to dig further.

I also see you have auth as default controller, which makes me think you have some sort of authentication going on?