We help IT Professionals succeed at work.

Could you clear why this Controller method isn't called as expected?

Eduardo Fuerte
on
Hi Experts


Could you clear why this Controller method isn't called as expected?

Accordingly with a previous question this form have 02 phases

1st: Upload a picture to a defined path - In my tests by using  
use Illuminate\Support\Facades\Storage;

Open in new window

It's perfectly locally uploaded.


2nd  The image url and details must to be saved at the table Vitrines

So, this method is called

Vitrine.prototype.salvar = function(){
    
  console.log ('JS salvar');  // Pass here
  
  $.ajax({
    url: '/admin/vitrine/salvar',
    method: "POST",
    data: {
      _token: $('input[name="_token"]').val(),
      id: $('#id').val(),
      url: $('#url').val(),
      inicio: $('#inicio').val(),
      termino: $('#termino').val(),
      title: $('#title').val(),
      description: $('#description').val(),
      points: $('#points').val()
    },
    error:function(data){
      hotsite.openModalCustom("Erro", data.responseText, "Entendi", "error");
    },
    success: function(data){
      hotsite.openModalCustom("Sucesso ao salvar", data.retorno, "Entendi", "success");
      hotsite.closeModal('mEditor');
      //hotsite.vitrine.filtrar();
    }
  });
};

Open in new window


img008
This method is configured at routes

Route::post('/admin/vitrine/salvar',['uses'=>'admin\VitrineController@salvar'])->middleware('cadastro');

Open in new window


But it doesn't seen it's called at VitrineController

 public function salvar(Request $request){
 
       info('Em salvar');

       //----------It must to stop here if it's called. But not.
        dd($request);
       //---------------------------------------------

        $id=$request->input('id');
        $url=$request->input('url');
        $inicio=$request->input('inicio');
        $termino=$request->input('termino');

        $titulo = $request->input('title');
        $descricao = $request->input('description');
        $pontuacao = $request->input('points');
  
        try{
            if($id != '' &&  $id > 0){
              $vitrine = Vitrine::find($id);
            }
  
            if(empty($vitrine)){
              $vitrine = new Vitrine();
              $vitrine->created_at=new \DateTime();
            }
                
            $vitrine->title = $titulo;
            $vitrine->description = $descricao;
            $vitrine->points = CustomFuncs::brancoParaNulo($pontuacao);
            $vitrine->url = $url;
            $vitrine->validity_start = empty($inicio) ? null : CustomFuncs::formatarDataUS($inicio);
            $vitrine->validity_end = empty($inicio) ? null : CustomFuncs::formatarDataUS($termino);
            $vitrine->updated_at=new \DateTime();
            
            info($vitrine);
            
            
            $vitrine->save();
  
        } catch (\Exception $e) {
            return response()->json(new Resultado(true,'','Erro ao salvar o vídeo'), 400);
        }
  
        return response()->json(new Resultado(false,'','Vídeo cadastrado com sucesso'));
    
    }

Open in new window



And a success message is presented by the above JS code:
 img007

But nothing is saved at table Vitrines.


Could you check?

Thanks in advance
Comment
Watch Question

Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
Hi Eduardo,

First step is to make sure that your routes are correctly configure. From a terminal in the root of your application run the following artisan command:

php artisan route:list

That will show you all the routes you have configured. It will show you the method (POST/GET), the URL (admin/vitrine/salvar) and the name of the Controller@method that it points to. Make sure the correct route exists in your app.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Hi Chris

Something wrong occured:

  img009
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
That looks like you're trying to use Passport but haven't installed it correctly. You need to run

php artisan passport:install

so that the correct keys are created in the storage folder.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Exact!

I'm seeking the routes....
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Once you've got Passport isntalled correctly, then run the php artisan route:list command and you'll see a list of all your routes.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
So, I have:

For vitrines:
img010
For videos:
img011
Something different on the middleware...
Eduardo FuerteDeveloper and Analyst

Author

Commented:
The routes:

/* Vitrine  EF 2020 */
Route::get('/vitrine',['uses'=>'VitrinesController@index'])->middleware('cadastro');
Route::get('/vitrine/{id}',['uses'=>'admin\VitrineController@obter'])->middleware('cadastro');
Route::get('/vitrine/{id}/obter',['uses'=>'admin\VitrineController@obter'])->middleware('cadastro');
Route::post('/vitrine/salvarescolha',['uses'=>'VitrinesController@salvarEscolha'])->middleware('cadastro');
// Rota adicionada:
Route::post('/admin/vitrine/salvarVitrine',['uses'=>'admin\VitrineController@salvarVitrine'])->middleware('cadastro');
Route::post('/admin/vitrine/salvar',['uses'=>'admin\VitrineController@salvar'])->middleware('cadastro');



/* Vídeos */
Route::get('/videos', 'VideoController@index')->middleware('cadastro')->middleware('auth');
Route::get('/videos/{type}/filtrar', 'VideoController@filtrar')->middleware('cadastro')->middleware('auth');
Route::post('/videos/watched', 'VideoController@watched')->middleware('cadastro')->middleware('auth');
Route::get('/videos/sugestao', 'VideoController@sugestionIndex')->middleware('cadastro')->middleware('auth');
Route::post('/videos/sugestao', 'VideoController@sugestion')->middleware('cadastro')->middleware('auth');

Open in new window


Isn't it a matter of to change the middleware to 'auth' ?
Eduardo FuerteDeveloper and Analyst

Author

Commented:
This project has a folder routes

And the file  admin.php controls it too (I guess)

 img012
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
OK.

I think we're missing a vital part of your Route setup. In the screenshots you've just shown me, your route is setup like

Route::get('/videos/obter', 'VideoController@obter');

But when you run route:list, it clearly shows that the routes have the admin url prefix and the admin namespace added to them. This leads me to believe that somewhere in your routes setup, you're declaring a prefix('admin') and a namespace('admin'), but you haven't shown any of that.

That's a vital part of getting your routes set up correctly.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
I don't know if I really understood what you meant....

In your example the method "obter" is under admin folder:

img013
img014
Eduardo FuerteDeveloper and Analyst

Author

Commented:
I adjusted the web.php to  consider the route outside \admin\    by using VitrinesController ( at participants area like all the other existents methods)

Route::post('/vitrine/salvarVitrine',['uses'=>'VitrinesController@salvarVitrine'])->middleware('cadastro');
Route::post('/vitrine/salvar',['uses'=>'VitrinesController@salvar'])->middleware('cadastro');

Open in new window


The method is reached.

My dificulty now is to understand why does it subtly exit the method returning to JS as OK aparently doing nothing - doesn't allow trace nothing.

If I force an error I see it capture it.

     public function salvar(Request $request){
     
     
         aaa;
        
        //info('xxx');
       
        dd('aaa');

Open in new window


img015
But that's would be another subject...
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Chris

Thank you for the help!
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
No worries Eduardo. Glad I could help.