Form in Laravel 5.2 does not find route - php

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.

Related

Route [controller.create] is not defined when I've already defined the Resource Controller in my Web.php

So I'm working with two Resource Controllers (I'm not sure if that's the problem but if it is, please let me know); one of them is StudentController and its URL is "/main". It's working as it should, perfectly. Now I'm creating the 2nd Resource Controller which name is TeacherController, and its URL is "teacher/main" - i.e its files are in the 'teacher' folder. This is my web routes file:
Web.php:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('/main','StudentController');
Route::get('profile','ProfileController#index')->name('profile');
Route::get('profile/edit','ProfileController#edit')->name('profile/edit');
Route::put('profile/edit/{id}','ProfileController#update')->name('profile.update');
Route::resource('teacher/main','TeacherController');
Now when I go to teacher/main - it says that "teacher.main.create" is not defined. That is the route that I've used on my create button i.e
{{ link_to_route('teacher.main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}
And this is the create() function in my Resource Controller:
public function create()
{
return view ('teacher/create');
}
From my understanding of Laravel Resource Controllers, I shouldn't have to define each Resource Controller function individually as Laravel should automatically detect each of them since I've already defined Route::resource('teacher/main','TeacherController'); in my Web Routes. What am I doing wrong here? This is just the first function it's getting, then there are other functions like Edit/Destroy that will also pop-up route errors and I don't want to go through with defining each of them individually. Help is highly appreciated.
Try to use in group with prefix and as
Route::group(['prefix' => 'teacher','as'=>'teacher.'], function () {
Route::resource('main','TeacherController');
});
Now you can use teacher.main.create
{{ link_to_route('teacher.main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}
I just tested this locally, and seems like you're using wrong name for your resource route. I set the routes exactly as in your example, and this is what I got:
So if you wanted to use the create route, you'd do it like this:
{{ link_to_route('main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}
On the other hand, if you want to keep the names you wanted, you can set a custom name for each of your resource route, like this:
Route::resource('teacher/main', 'TeacherController', [
'names' => [
'index' => 'teacher.main.index',
'create' => 'teacher.main.create',
// etc...
]
]);
Hope this helps. Happy coding.
Route::resource('teacher-main','TeacherController');
now you can access
teacher-main.index

Laravel 5.4 routes/api.php POST MethodNotAllowedHttpException

I am writting APIs for android/ios using Laravel 5.4. My simple webservice signUp which is working perfectly on localhost not working on live server and giving
MethodNotAllowedHttpException on POST methods
GET call works perfect .
Route code
Route::post('/signUp',['uses'=>'API_UserController#userSignUp']);
Attached is screen shot link of my postman.
https://i.stack.imgur.com/060IF.png
here is quick example of flow try this
<form action="{{url('v1/userSignUp')}}" class="validation" method="post"
accept-charset="utf-8">
{{ csrf_field() }}
<div>
// your required form field
<input type="submit" value="Add Category" class="btn btn-primary" />
</div>
</form>
and in your route add this
Route::get('/signUp', function () {
return view('yourviewpagename');
});
Route::post('/signUp','API_UserController#userSignUp');
and in your API_UserController
public function userSignUp()
{
dd(request->all());
}
#Mujeeb Ur Rehman Post method is use to send data and get method is use to access data in view or just to render view.
eg:-route:post('/home',xxController#xx);
route:get('/home1',xxController#xx);
error which you are getting is because suppose if you url it like this localhost/xx/home your framework think to access data or post data framework get confuse in this
just use like this
Route::group(['prefix' => 'v1'], function () {
Route::post('/signUp','API_UserController#userSignUp');
});
when you hit via postman tour URL is http://your_domain/api/v1/signUp so just add a prefix it will automatically add with your URL. in route group u can also use many other things like namespace, middleware etc. Example :
Route::group(['namespace' => 'any_depend_on_your_project_structure', 'middleware' => 'any_middleware', 'prefix' => 'v1'], function() {
// your routes
});

TokenMismatchException after using web middleware

im adding csrf_field to all my forms by default and it was working fine , i decided to store some data in session so i've grouped some routes and used web middlewar on them
Route::group(['middleware' => ['category' , 'web']], function () {
Route::get('/', 'HomeController#index');
Route::get('/dashboard', 'DashboardController#index')->name('dashboard');
})
now when i submit a form i get this error
TokenMismatchException in VerifyCsrfToken.php line 67:
but they work fine if i remove web middleware !!
im using database drive for my sessions ... i dont know if that's relevant
Remove web middleware, that should fix the problem.
Since 5.2.27 web middleware applies automatically to all routes (in 5.3 to all routes in routes/web.php) and you shouldn't add it manually.
If the form is not token field _token
<form method="POST" action="">
{{ csrf_field() }}
...
</form>

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

Laravel homestead suddenly stopped using 8000 port suffix when submitting forms

I have a form that looks like this:
<form method="POST" action="{{ route('flyers.store') }}" enctype="multipart/form-data" class="col-md-6">
#include('flyers.form')
</form>
Throughout the entirety of the project, this worked. It would post to my local development url http://projectflyer.dev:8000/flyers.
Suddenly it's posting to http://projectflyer.dev/flyers.
I'm not sure what would cause this. Any suggestions?
Another interesting development: When I type http://projectflyer.dev:8000/flyers directly into the browser, it redirects to http://projectflyer.dev/flyers.
Routes file looks like:
<?php
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('pages.home');
});
Route::resource('flyers', 'FlyersController');
Route::get('{zip}/{street}', 'FlyersController#show');
Route::post('{zip}/{street}/photos', ['as' =>'store_photo_path', 'uses' => 'FlyersController#addPhoto']);
});
This happens as a result of having a route post to 'flyers' when also having a public directory called 'flyers'.

Categories