I have routes setup as
// ----------------------- USER ROUTES -----------------------
Route::group(['prefix' => 'v1/user', 'middleware' => 'throttle:5'], function(){
Route::post('login', 'UserController#login');
});
Route::group(['prefix' => 'v1/user', 'middleware' => 'throttle'], function(){
Route::post('checkuser', 'UserController#checkuser');
Route::post('checkmail', 'UserController#checkmail');
});
HTML as
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
{!! csrf_field() !!}
And I am getting Method not found error
I know somewhere route is not correct, but how to correct it?
In your case it should be {{ url('v1/user/login') }}.
To avoid using url() you can leverage named routes.
https://laravel.com/docs/5.2/routing#named-routes
Related
I want to generate a random password on button click but it leads to Error 404
The Form
<form style="position: relative; left: -15px" action="{{ route("dashboard.users.generate-app-password-store") }}" method="POST">
#csrf
<button type="submit" class="btn btn-primary float-right mt-2">
{{ trans("translation.generate-password") }}
</button>
</form>
The Controller Function
public function generateAppEmailPasswordStore(Request $request)
{
$user = User::findOrFail($request->uid);
$user->app_email_password_store = Hash::make(Str::random(8));
$user->app_email_password_store = $request->app_email_password_store;
$user->save();
Alert::flash(trans('translation.email-account-created'));
return redirect()->route('dashboard.users.profile', ['id' => $user->id]);
}
The route (prefix => dashboard is the main route group)
Route::group(['prefix' => 'user', 'middleware' => 'checkRole:admin'], function () {
Route::post('/generate-app-password-store', 'Dashboard\UsersController#generateAppEmailPasswordStore')->name('dashboard.users.generate-app-password-store');
});
The error
You are probably recieving 404 because of this line in the controller method.
$user = User::findOrFail($request->uid)
Check if you get the correct value for $request->uid or if there is a user with the given ID.
Otherwise it will throw a 404 error.
either change your dashboard.user.generate-app-password-store (instead of dashboard.users.generate-app-password-store) in your view
or
change your route prefix to users (instead of user)
The problem seems to be that you mismatch the " usage.
action="{{ route("dashboard.users.generate-app-password-store") }}"
should be
action="{{ route('dashboard.users.generate-app-password-store') }}"
The reasoning is that you close the action too early.
action="{{ route("
I'm trying to implement multi auth guards with AdminLTE in Laravel (version 6.x) and I have trouble to pass the guard (as $url variable) in login/register forms.
My API works fine without AdminLTE, and it works fine with normal user with AdminLTE.
I can display Admin login/register forms by taping "/login/admin" and "register/admin" in the address bar, but when I click login/register button I get "404 Not Found" page and in the address bar it displays "/login/$url"
I have changed the login.blade.php and register.blade.php in the following directory:
vendor/.../view/login.blade.php and vendor/.../view/register.blade.php
LoginController.php:
public function adminLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {
return redirect()->intended('/admin');
}
return back()->withInput($request->only('email', 'remember'));
}
RegisterController.php:
protected function createAdmin(Request $request)
{
$data = $request->all();
$this->validateAdmin($data)->validate();
$admin = Admin::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
return redirect()->intended('login/admin');
}
routes/web.php:
Route::post('/login/admin', 'Auth\LoginController#adminLogin');
Route::post('/register/admin', 'Auth\RegisterController#createAdmin');
login.blade.php:
#isset($url)
<form action="{{ url('login/$url') }}" method="post">
#else
<form action="{{ url(config('adminlte.login_url')) }}" method="post">
#endisset
register.blade.php:
#isset($url)
<form action="{{ url('register/$url') }}" method="post">
#else
<form action="{{ url(config('adminlte.register_url')) }}" method="post">
#endisset
You're hard coding the $url as string as oppose to using it as a variable
This is due to using single quotes in the blade view files in the url() helper function argument
Just change it to double quotes
login.blade.php:
#isset($url)
<form action="{{ url("login/$url") }}" method="post">
#else
<form action="{{ url(config('adminlte.login_url')) }}" method="post">
#endisset
register.blade.php:
#isset($url)
<form action="{{ url("register/$url") }}" method="post">
#else
<form action="{{ url(config('adminlte.register_url')) }}" method="post">
#endisset
Changing files in vendor folder is redundant because your changes will be overridden on composer update/install
From the docs
If you have published and modified the default master, page views or login views, you will need to update them too.
Option 1:
Make a copy of the views you modified.
Publish the views again, using
php artisan vendor:publish --provider="JeroenNoten\LaravelAdminLte\AdminLteServiceProvider" --tag=views
Redo the modifications you did.
Hope this helps
First of all I want to say that my problem is very similar to this question, only the answer is not working in my case.
I am trying to connect my form action to my UserController. it's an update avatar function. Here's what it look like.
in my profile/show.blade
<div class="col-md-12 justify-content-center">
<form action="{{ action('UsersController#update_avatar') }}" method="post" enctype="multipart/form-data">
#csrf
</form>
</div>
UsersController
public function avatar()
{
$user = Auth::User();
return view('dashboard.profile'.$user->id,compact('user',$user));
}
public function update_avatar(Request $request){
$request->validate([
'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$user = Auth::User();
$folder = 'avatars';
Storage::delete($folder.'/'.$user->avatar);
$avatarName = $user->id.'_avatar'.'.'.request()->avatar->getClientOriginalExtension();
$request->avatar->storeAs('avatars',$avatarName);
$user->avatar = $avatarName;
$user->save();
return back()
->with('success','You have successfully upload image.');
}
routes/web.php
Route::get('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#avatar');
Route::post('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#update_avatar');
here's the error I am receiving
Thank you so much in advance!
Change these routes:
Route::get('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#avatar');
Route::post('dashboard/profile/{{profile}}', 'Dashboard\\UsersController#update_avatar');
To these:
Route::get('dashboard/profile/{{profile}}', 'UsersController#avatar')->name('user.avatar');
Route::post('dashboard/profile/{{profile}}', 'UsersController#update_avatar')->name('user.update_avatar');
And in your form use route instead of action
<form action="{{ route('user.update_avatar') }}" method="post" enctype="multipart/form-data">
#csrf
</form>
You can try do something like this:
Route::post('dashboard/profile','Dashboard\UsersController#update_avatar')->name(profile.update);
and use it in the form like this:
<form action="{{ route('profile.update') }}">
I want to insert data in database. i found a problem. I have 2 blade.php page. 1. dashboard.blade.php and another is class.blade.php.
When i use in my form action dashboard.blade.php its working. but when i used in action class.blade.php its not working. i can't found this problem. Here is my form action :
<form class="form-horizontal" role="form" method="POST" action="{{ url('/dashboard') }}">
{!! csrf_field() !!}
when i changed it
<form class="form-horizontal" role="form" method="POST" action="{{ url('/class') }}">
{!! csrf_field() !!}
then its not working.
Here is my controller
public function showclass(Request $request)
{
$randomnumber = rand(50001,1000000);
$classrooms = new Classrooms();
$classrooms->class_name = $request['class_name'];
$classrooms->subject_name = $request['subject_name'];
$classrooms->section = $request['section'];
$classrooms->class_code = $randomnumber;
$classrooms -> user_id = Auth::user()->id;
$classrooms -> save();
Flash::success('Your status has been posted');
//return redirect(route('dashboard'));
return view('dashboard', array('classroom' => Auth::user()) );
}
There is an error in your Route. You have used showdata method instead of showclass. Just change it like below:
Route::post('/class', [
'uses' => 'classroom#showclass',
'as' => 'classrooms']);
Note: Make sure to specify specific method while defining the routes.
I'm learning Laravel and I have a problem returning the old inputs to the form.
ERROR:
TokenMismatchException in VerifyCsrfToken.php line 67:
ROUTES - all in the file
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('artigo');
$artigo = \App\Comentario::find(2)->artigo;
var_dump($artigo->title);
$comentarios = \App\Artigo::find(1)->comentario;
foreach($comentarios as $comentario){
var_dump($comentario->body);
}
});
Route::post('/', function(){
$rules = array(
'title'=>'required|max:10',
'body'=>'required|max:4'
);
$validator = Validator::make($_POST,$rules);
if($validator->fails())
return Redirect::to('/')->withInput()->withErrors($validator->errors());
return 'yooo';
});
});
BLADE VIEW
<!DOCTYPE html>
<html>
<body>
<form method="post" action="/">
<input type="text" name="title" placeholder="titulo" value="{{ old('title') }}">
<input type="text" name="body" placeholder="body">
<input type="submit" value="go">
</form>
</body>
</html>
Any help?
ATENTION: im not using sessions yet
Assuming you are using version 5.2 this is might be because your requests are not utilising the sessions. In Laravel 5.2 Sessions are available only if you are using the web middleware.
You should include all routes using sessions within a middleware group called web which is defined in app/Http/Kernel.php under $middlewareGroups
Route::group(['middleware' => ['web']], function () {
// Routes using sessions
});