I dev laravel 5.4 on windows 10 with xampp (Apache + PHP 5.6.28) and I move my project to MacBook and I using Mamp pro (Apache + PHP 5.6.28).
I have a problem,Can't POST Request all form action.
It's show,
TokenMismatchException
in VerifyCsrfToken.php (line 68)
.....
I try,
Composer update
remove all file in session
php artisan cache:clear
browser clear cache
but can't resolve.
How can I do ?
add csrf token in your form.
<form>
{{ csrf_field() }}
</form>
Here is documentation about csrf protection.
Also you can use laravelcollective forms. They have csrf protection by default. You can easly install this package from here
Related
I am using the livewire image preview temporaryUrl() function
<input wire:model="image" type="file" accept="image/*">
<img class="input-img-preview" src="{{$image->temporaryUrl()}}">
It is working fine when I do the command:
php artisan optimize
But the language routes are turning into 404 error!
And when I use this command:
php artisan optimize:clear
The language routes are working fine but the image preview crashes.
BTW I am using mcamara/laravel-localization
I have a pretty simple livewire component that helps a user store a vanity URL
<form wire:submit.prevent="save">
<div class="form-group form-inline">
<span>{{ config('app.url') }}/request/</span>
<input type="text" id="vanity" wire:model.defer="vanityUrl" name="vanityUrl">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">#if($saved) Saved #else Save #endif</button>
<button type="button" id="copy-vanity" class="btn btn-secondary">Copy</button>
</div>
</form>
And the save action is pretty simple as well.
public function save()
{
$user = Auth::user();
$this->validate();
$user->update(['vanity_slug' => Str::slug($this->vanityUrl)]);
$this->vanityUrl = Str::slug($this->vanityUrl);
$this->saved = true;
}
This works locally but not in production. I am using Forge, php7.4, L8, its all the same between the environments except the URLs. I have livewire:scripts and livewire:styles loaded, and again it works locally but not in prod.
In prod, the page reloads. I have been trying to figure this out for a couple days now... driving me crazy. TIA
Cached Views
The most typical issue when Livewire isn't working after deploying to a production environment (or for that matter, after setting it up in localhost as well), is that your views, specifically layouts/app.blade.php is cached before the #livewireScripts directive is loaded. This means that it will not render #livewireScripts or #livewireStyles as the component, but will output the literal string.
This is fixed by simply running php artisan optimize:clear, to clear your cache (specifically the view cahce).
Missing Installation of Livewire
However, there are some cases where Livewire is not installed - or not installed properly. You can ensure that Livewire is installed through composer by running composer show -D while in the root directory of your Laravel application. Look for livewire/livewire. If its not there, then install it (see https://laravel-livewire.com/docs/2.x/installation). To install Livewire through composer, run the following command.
composer require livewire/livewire
Using Alpine.js v3 and Livewire v2w
If you have updated to Apline.js v3, you need to use Livewire v2.5 or higher. Simply update your Livewire version.
Faulty Installation of Livewire
The last scenario I've heard of so far with this kind of problem, is that Livewire is installed - but its still not working properly. This is the most uncommon from what I have seen so far. This happens when the scripts that Livewire hooks into is not working as intended (say, a form with wire:submit.prevent="submit" is submitting the actual form instead of sending the AJAX request), but Livewire is installed and #livewireScrtips is rendering as it should. For whatever reason this happens, I'm not sure (as I haven't experienced it first hand), but its quite simple to fix.
The solution for these scenarios is to reinstall Livewire - and you do that by simply removing and require the package again. After the reinstall, its a good idea to clear the cache, just to ensure that the view is not cached without the Livewire-directives.
composer remove livewire/livewire && composer require livewire/livewire && php artisan optimize:clear
The Laravel application I set up is working flawlessly on my CentoS 7 dev server. Specifically: logging in and logging out with:
php artisan make:auth
However, when I move it to my live server, the "logout" function stops working. I click my logout button in Chrome,
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
it redirects to my login screen through my LoginController function,
public function logout(Request $request) {
Auth::logout();
return redirect('/login');
}
but when I type in a URL that is supposed to be secure, that URL loads. If I refresh the page, then it actually logs me out.
In Firefox, nothing happens at all when when the logout button is clicked on the blade.
Also, it may be worth noting that in Chrome on my dev server, the cookie set for the page has an Expires/Max-Age of 2hours from now (today)
2019-01-15....
which is correct.
In Chrome on my live server, the cookie Expires/Max-Age is
1969-12-31....
Save for my APP_URL, database credentials and mail server, my .env files are identical. I have changed nothing else -just uploaded the files. The rest of the functionality on the site works perfect.
I have tried many different configurations and searches and still no luck. Thank you for reading this.
It turns out on my live server in my .htaccess file, I was modifying the Cache-Control settings:
<IfModule mod_headers.c>
<FilesMatch ".(php)$">
Header set Cache-Control "max-age=86400"
</FilesMatch>
</IfModule>
Once I removed these lines, my Laravel app now works.
It directly show #csrf & #method('PUT') on the page
You may use the #method Blade directive to generate the _method input:
<form action="/foo/bar" method="POST">
#method('PUT')
</form>
These code does not work.
These directives are coming in Laravel 5.6 as it was mentioned by Laravel News.
As I am writing this answer, Laravel 5.6 have not yet been released.
You can do this to install a fresh version of Laravel 5.6:
composer create-project laravel/laravel your-project-name-here dev-develop
Token is important for security in Laravel. Can any one tell me how to use it in blades of Laravel 5.1
I used the following method:
<input type="hidden" value="csrf_token()" />
But i want it to be in the form :
Form::(some code)
Laravel 5.1 haven't out of the box the Form component. You have to install from https://laravelcollective.com/docs/5.1/html
{!! Form::token() !!} will be helpful in Laravel 5.1.