Laravel Routing does not Controller - php

I want to access my controller and show and log the test messages.
I have been trying to access my controller via a route but it doesn't seem to work.
It's my first time using Laravel so I hope anyone can help I have been struggling with this problem for a couple of hours.
It hasn't been showing error messages, but I can't find any mistakes in my syntax I tried finding if there might be possible problems in my php.ini files, but I don't know.
the form
<form method="post" action="{{ Route('saveItem') }}" accept-charset="UTF-8">
{{ csrf_field() }}
<label for="listItem">New Todo Item</label><br>
<input type="text" name="listItem">
<button type="submit">Save item</button>
</form>
Route
Route::post('/saveItemRoute', [TodoListController::class, 'saveItem'])->name('saveItem');
Controller
class TodoListController extends Controller
{
public function saveItem(Request $request) {
echo("<h1>victory2</h1>");
\Log::debug("TodoListController");
\Log::info(json_encode($request->all()));
$newListItem = new ListItem;
$newListItem->name = $request->ListItem;
$newListItem->is_complete = 0;
$newListItem->save();
return view('welcome');
}
}
After trying again it seem like the rout doesnt function / do anything and i cant figure out why

I know what whent wrong after changes to routing i needed to do this line in the cmd
php artisan route:clear
the first time i tested the routing code but i did not know after changes you need to clear the route

Related

Laravel match and any method

I´m working with Laravel building a web app. This app will contain a FORM that only asks a name of a requisite of a client that will be added in to a database. I´m trying to obtain the ID of the client by 'GET' method and the title of the requisite by 'POST' method. I´ve tryied to use 'MATCH' metchod but that brings me errors.
These is the code in the view:
<form action="{{ route('folders.store') }}" method="post">
#csrf
#method('match')
...
</form>
These in the web:
Route::match(['get', 'post'], 'folders/store', [PartnerController::class, 'store'])-> name('folders.store');
and these in the controller:
public function store(Request $request)
{
#dump($request);
return $request;
}
In the last I´m just trying to verify want I´m doing. jaja.
Thank you.
I´ve tryied using method 'any', with no results.
I´ve changed the 'post' to 'get' method but it´s not a really secure option.
I think I don´t really understand what I´m doing at all.
you're out of conventions but anyway if you're user logs in you can get the id with Auth::id(), but if not, go with this
Route::post('folders/store/{$id}', [PartnerController::class, 'store'])-> name('folders.store');
then in form
<form action="{{ route('folders.store',$id) }}" method="post">
#csrf
...
</form>
and in the controller
public function store(Request $request,$id)
{
dd($request);
return $request;
}

Can not get my data from my form in Laravel?

I am new to Laravel, I am trying to have a simple example, but I am getting a 419 error, I dont know why it shows up but I will expalin what I did,
I created a simple Controller and I called it FormController with the command line :
php artisan make:controller --resource FormController
In my web.php I added this :
Route::resource('form','FormController');
my view has a simple form in it :
<form action="/form" method="POST" >
<input type="text" name="cih">
<input type="submit">
</form>
I open my view with the create method :
public function create()
{
return view('contact');
}
I want that when I submit my form I get my data, so I use my 'store' method :
public function store(Request $request)
{
return $request->all();
}
But instead of getting it, I get 419 message, and my session has expired etc ..
I followed a course and that what the teacher was doing I believe nothing more, so I would appreciate any help, I need it.
Thank you
You need to include the CSRF token while submitting a form since the 'VerifyCsrfToken' middleware is enabled by default for the web routes in App/Http/Kernal.php.
<form action="/form" method="POST" >
#csrf
<input type="text" name="cih">
<input type="submit">
</form>
And, Welcome to Laravel!

MethodNotAllowedHttpException in RouteCollection.php line 218:4

I will get MethodNotAllowedHttpException when submitting a form in laravel
Html file
<form method="POST" action="/cards/{{$card->id}}/notes">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<textarea name="body" class="form-control"></textarea>
<button type="submit">Add Note</button>
</form>
routes.php
Route::post('cards/{card}/notes','NotesController#store');
NotesController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class NotesController extends Controller
{
public function store()
{
return request()->all();
}
}
Make sure you don't have a route, say a Route::post with a parameter that lies in front of the route you are trying to hit.
For example:
Route::post('{something}', 'SomethingController#index');
Route::post('cards/{card}/notes', 'NotesController#store');
In this case, no matter what you try to send to the cards route, it will always hit the something route because {something} is intercepting cards as a valid parameter and triggers the SomethingController.
Put the something route below the cards route and it should work.
MethodNotAllowedHttpException is thrown when no matching route (method and URI) was found, but a route with a matching URI but not matching method was found.
In your case, I guess the issue is because URI parameters differ between the route and the controller.
Here are two alternatives you can try:
Remove the parameter from your route:
Route::post('cards/notes','NotesController#store');
Add the parameter to your controller:
public function store($card)
{
return request()->all();
}
I have tried to solve this error in lumen and it took me quite a lot of time to figure out the problem.
The problem is with laravel itself.
Sometimes if you have another route like GET device/{variable}, laravel stops in this first route...
So what you need to do is change the route POST device to POST device/add
This link helped me a lot

laravel 5, form submit redirect to unknown url value

I just wondering in my project. I have a form that can be access at localhost/app/esetting/mymail and this is the code in the view:
....
<form action="{{ url( 'app/esetting/emailautomsave' ) }}" class="form-horizontal" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
...
<input type="submit" value="save">
</form>
but when I try to click for form submission, I expect it to go to app/esetting/emailautomsave and calls it controller which is on my SettingsController.php.
public function postEmailautomsave(Request $request){
...
}
but it redirects to localhost/app/mymail? and give me this error:
NotFoundHttpException in Controller.php line 93:
Controller method not found.
this sounds weird on my end. can anyone have an idea about this? I am sure I have done the right this specially on my routes.php
Route::group([ 'prefix' => 'app', 'middleware' => 'auth' ], function() {
....
Route::controller('esetting', 'SettingController');
Route::get( 'esetting/mymail', 'SettingController#viewEmailAutom' ); // view for the form to display
....
Just simply define:
Route::post('esetting/mymail/emailautomsave', 'SettingController#postEmailautomsave');
I know that You'll say: "I've defined Route::controller, so it will look for it atuomatically."
But for me is best to have routes defined exactly.
also if:
it redirects to localhost/app/mymail? and give me this error:
NotFoundHttpException in Controller.php line 93: Controller method
not found.
maybe it means that some middleware is redirecting You there?
You can check it by simply doing this:
public function postEmailautomsave(Request $request){
die('test');
...
}
if it will redirect so it mean that some function was called before and redirect browser to app/mymail.
If you use Route::controller you need to define you methods according to the route.
E.g.
Route::controller('esetting', 'SettingController');
Will look for SettingController#getIndex when you visit http:://yoursite.com/esetting
To reach SettingController#postEmailAutomsave() you would need to go to the route http:://yoursite.com/esetting/emmail/automsave
I have not used Route::controller myself, I got used too (and now prefer) naming them individually.
But this should do the trick.
Laravel 5.1 docs

Form in Laravel 5.2 does not find route

I'm trying to make a simple post through a form, the route exists and the token is there, but when a submit is made always returns '404 Not Found'.
Route:
Route::group(['middleware' => ['web']], function () {
Route::post('/cadastro', 'UsuarioPost#cadastro');
});
UsuarioPost Controller:
class UsuarioPost extends Controller
{
public function cadastro(Request $request)
{
return dd($_POST);
}
}
View with the form:
<form id="f_cadastro" method="POST" action="{{ URL::to('/cadastro') }}">
{{ csrf_field() }}
<button type="submit">Cadastrar</button>
</form>
Is there something new from laravel 5.1 to 5.2 in form submiting?
This used to work fine in the previus version, even without the group in the route.
I suggest you to use named routes instead of this strategy, is more convenient.
Route::get('/profile', [
'as' => 'profile.index',
'uses' => 'ProfileController#index',
]);
And then you can generate the url from your views or codes using only
{{ route('profile.index') }}
So, finally working.
The deal was with apache, and not laravel. Apaches httpd.conf file (apaches directory/conf/httpd.conf) had AllowOverride disabled as default, wich is needed by laravel. So I had to change every single "AllowOverride none" for "AllowOverride all", and removed the line "Require all denied".
Having my apache DocumentRoot already set to the public folder from my project everthing worked fine.

Categories