Eduardo Fuerte
asked on
Could you clear why this Controller method isn't called as expected?
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
2nd The image url and details must to be saved at the table Vitrines
So, this method is called
This method is configured at routes
But it doesn't seen it's called at VitrineController
And a success message is presented by the above JS code:
But nothing is saved at table Vitrines.
Could you check?
Thanks in advance
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;
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();
}
});
};
This method is configured at routes
Route::post('/admin/vitrine/salvar',['uses'=>'admin\VitrineController@salvar'])->middleware('cadastro');
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'));
}
And a success message is presented by the above JS code:
But nothing is saved at table Vitrines.
Could you check?
Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
php artisan passport:install
so that the correct keys are created in the storage folder.
ASKER
Exact!
I'm seeking the routes....
I'm seeking the routes....
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.
ASKER
The routes:
Isn't it a matter of to change the middleware to 'auth' ?
/* 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');
Isn't it a matter of to change the middleware to 'auth' ?
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.
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'
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.
ASKER
ASKER
I adjusted the web.php to consider the route outside \admin\ by using VitrinesController ( at participants area like all the other existents methods)
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.
But that's would be another subject...
Route::post('/vitrine/salvarVitrine',['uses'=>'VitrinesController@salvarVitrine'])->middleware('cadastro');
Route::post('/vitrine/salvar',['uses'=>'VitrinesController@salvar'])->middleware('cadastro');
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');
But that's would be another subject...
ASKER
Chris
Thank you for the help!
Thank you for the help!
No worries Eduardo. Glad I could help.
ASKER
Something wrong occured: