Laravel 5.1 resource route does not work properly - php

// routes.php
Route::resource('/image', 'ImageController');
Route::get('/create', 'ImageController#create');
Route::post('/store', 'ImageController#store');
// create.blade.php
{!! Form::open(array('url' => '/store', 'method'=>'POST')) !!}
.......
{!! Form::close() !!}
Here if i don't write these two lines (Route::get('/create', 'ImageController#create'); Route::post('/store', 'ImageController#store');)
The resource routing of create and store does not work and show some errors.
Why this happens? Thanks in advance.

When creating resource route you don't have to create individual routes. Because all RESTfull default routes will be created for you automatically.
You just need following route
Route::resource('image', 'ImageController');
then change form as below
{!! Form::open(array('route' => array('image.store'))) !!}
.......
{!! Form::close() !!}
Read More

Related

Laravel collective action

I am beginner at laravel. I want to use laravel collective (on laravel 9 version). And actually i want to use action. I write it in my blade:
{!! Form::open(['action' => 'PostsController#store', 'method' => 'POST']) !!}
{!! Form::close() !!}
and when i open it in browser it gives me error Action PostsController#store not defined.
i do not know. help me. i try to googled it but did not find. also i tried [PostsController::class] but not worked. i tried everything what i know
Best Solution - Using named routes
From documentation of Laravel Collectives you may come to know that you can use named routes in your code. So, we better use named route.
{!! Form::open(['route' => 'route.name', 'method' => 'POST']) !!}
{!! Form::close() !!}
Alternatives
You could either string including the namespace of controller with it to work
{!! Form::open(['action' => 'App\Http\Controllers\PostController#store', 'method' => 'POST']) !!}
{!! Form::close() !!}
or you could modify your RouteServiceProvider file and change the controller namespace there. protected $namespace = 'App\\Http\\Controllers'; this approach allows you to use 'ControllerName#method' controllers created in App\Http\Controllers directory.
The other way around is just call the route name.
<form action="{{ route('your.route.name') }}">
...
</form>

Laravel 5.2 token mismatch

I'm updating a Laravel 5.1 site to Laravel 5.2.39. I had a lot of issues with the "approved" upgrade process (in particular the middleware) so I ended up creating a new Laravel 5.2 site and just moving over my views, models and controllers from the old site. It seems to be working, except when I submit any form I get a token mismatch exception:
TokenMismatchException in VerifyCsrfToken.php line 67:
My forms have the token (using {{ csrf_field() }}) in them:
<div class="form-group">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', old('name'), ['placeholder' => 'Name', 'class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email') !!}
{!! Form::email('email', old('email'), ['placeholder' => 'Email', 'class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('phone', 'Phone') !!}
{!! Form::text('phone', old('phone'), ['placeholder' => 'Phone', 'class' => 'form-control']) !!}
</div>
{{ csrf_field() }}
<div class="form-group">
{!! Form::submit('Submit', ['class' => 'btn btn-default btn-small']) !!}
<input type="reset" class="btn btn-primary btn-small" />
</div>
and I have cleared my cookies and also tried setting the config/session.php lifetime to a higher number to ensure it's not timing out.
This is running on a homestead box.
Any ideas what to try?
EDIT: here's the route in question
Route::resource('contact', 'ContactController');
EDIT2: here's the route file. I don't believe it is required to wrap the routes in the web middleware in Laravel 5.2-- however, I've tried it both ways with the same error.
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'AccountController#index');
Route::resource('account', 'AccountController');
Route::resource('contact', 'ContactController');
});
Dumping the $request->session()->token() in the controller's create action shows the current session token on the page with the form. This is the same token that is added to the form (checked with view source).
But when it gets to the Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::tokensMatch() class, I dump the session token and it's different than the form token-- meaning these will never match. Not sure what to expect here.
This is really annoying, but after everything else it appears this was a weird error caused by multiple Laravel sites on one Homestead box. I had set the session key differently and had cleared caches, but it still wasn't working.
Destroying the homestead box and rebuilding it appears to have fixed this issue. Thanks for all the comments and attempts at finding a solution.
Place your route within web Middleware. or in App\http\kernel copy all routes from web to protected $middleware array.
As long as you have your Form:open declaration, you no longer need the separate csrf_field line. Form::open should take care of it.
Are you using the laravel collective form package? The old illuminate package is no longer maintained.
As the previous user said, all routes also must be wrapped in the web middleware group, which also could be the problem.

Problems with route in Laravel

After clicking in view,
{!! Form::open(['method' => 'GET', 'route' => ['city', $city->id]]) !!}
{!! Form::submit('Check', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
system finds this route:
Route::get('city/{id}', 'CityController#show')->name('city');
which makes URL in web browser look like this:
http://localhost:8888/game/public/city/5?
problem is however, that when I click on another link for example:
<li>Reports </li>
I get URL in following format:
http://localhost:8888/game/public/city/home
instead of:
http://localhost:8888/game/public/home
Which is wrong since it doesn't work. How do I correct it?
Don't use relative URLs. You can use the url helper to generate a fully qualified url.
Home
Reference:
Laravel Docs - Helpers - url method

Define controller method as form action

I have a following code
{!! Form::open(array('action' => 'clientController#create')); !!}
it shouts that clientController#create is not defined unless I put some code into routes.php (this is my solution)
Route::post('clientRegistration', ['uses' => 'clientController#create', 'as' => 'registration']);
Everything then works fine, but I am not sure if I have found the correct solution, do I have to create a route for every form that will be created? I don't know why Laravel could not find a controller method without registering it in routes.php file
When you are calling an action Laravel searches through it's stored route to handler associations. This is done in routes.php
If you insert
Route::post('clientRegistration', ['uses' => 'clientController#create', 'as' => 'registration']);
in your routes.php you can do any of the following
{!! Form::open(array('action' => 'registration')); !!}
{!! Form::open(array('action' => 'clientController#create')); !!}
If you only stick with
Route::post('clientRegistration', 'clientController#create');
you can still use
{!! Form::open(array('action' => 'clientController#create')); !!}
More information here: https://laravel.com/docs/5.2/routing#named-routes
No matter what, you will need to inform Laravel about your route in routes.php, because otherwise it will not know how to handle the request.
I suggest
Route::resource('client', 'ClientController');
Please read
https://laravel.com/docs/5.2/controllers#restful-naming-resource-routes

Unable to create form tag in Laravel 5

Directory structure.
My current code is below
{{ Form::open(array('action' => 'SkillsController#store', 'method' => 'POST' }}
{{ Form::close() }}
I got the following error
Class 'Form' not found
{{!! Form::open(array('action' => 'SkillsController#store', 'method' => 'POST'}}
{{!! Form::close() !!}}
Still I am facing the same issue.
Reference
As of Laravel 5, Form helpers were removed and are now maintained and provided by the Laravel Collective.
The steps provided in their docs:
Add "laravelcollective/html": "5.1.*" to the require section of your composer.json file
Run composer update
Add Collective\Html\HtmlServiceProvider::class, to your array of providers in the congif/app.php file
Add 'Form' => Collective\Html\FormFacade::class, and 'Html' => Collective\Html\HtmlFacade::class, to your array of aliases in the config/app.php file
You'll then be able to use them in your views using {!! !!} in your views, like so:
{!! Form::open(array('url' => 'foo/bar')) !!}
//
{!! Form::close() !!}
In laravel 5 HTML, and forms not included you should include manually, can follow instructions in this link.
http://laravel.io/forum/09-20-2014-html-form-class-not-found-in-laravel-5

Categories