Laravel: Route [users.edit] not defined - laravel

I have created a demo application in Laravel 4, for some learning experience. The routes.php is as follows:
Route::get('/', function()
{
return View::make('hello');
});
Route::group(['prefix' => 'users', 'before' => 'auth'], function () {
Route::get('dashboard', function () {
$layout = View::make('users.dashboard');
$layout->title = "Dashboard";
$layout->content = View::make('users.dashboard');
return $layout;
});
Route::get('/all/', [ 'as' => 'users.index', 'uses' => 'UserController#all']);
Route::get('/create/', [ 'as' => 'users.create', 'uses' => 'UserController#create']);
Route::get('users/login', [ 'as' => 'users.login', 'uses' => 'UserController#getLogin']);
Route::post('users/login', [ 'as' => 'users.login', 'uses' => 'UserController#postSignin']);
Route::get('users/logout', [ 'as' => 'users.logout', 'uses' => 'UserController#getLogout']);
Route::resource('/','UserController');
});
Route::controller('users', 'UserController');
Here, I prefixed the "users". However, Trying logging in with/out correct login details, it shows only the login page instead of showing up the error message on the login page or dashboard.
Any suggestion? FYI,
The index.blade.php in app\views\users is below:
#extends('users.user')
#section('title', 'User Listing Page')
#section('description', 'This is the user listing page')
#section('main')
<h1>All Users</h1>
<p>{{ link_to_route('users.create', 'Add new user') }}</p>
<h1>Login</h1>
<p>{{ link_to_route('users.login', 'Login') }}</p>
<table>
<tr>
<td>{{ $users->links() }}</td>
</tr>
</table>
#if ($users->count())
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Phone</th>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td>{{ $user->username }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->phone }}</td>
<td>{{ $user->name }}</td>
<td>{{ link_to_route('users.edit', 'Edit', array($user->id), array('class' => 'btn btn-info')) }}</td>
<td>{{ Form::open(array('method' => 'DELETE', 'route' => array('users.destroy', $user->id))) }}
{{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}
</td>
</tr>
#endforeach
</tbody>
</table>
<table>
<tr>
<td>{{ $users->links() }}</td>
</tr>
</table>
#else
There are no users
#endif
#stop
The script login.blade.php (on the same location as above) as follows:
#extends('users.user')
#section('main')
<h1>Login</h1>
{{ Form::open(array('route' => 'users.login')) }}
<ul>
<li>
{{ Form::label('username', 'Username:') }}
{{ Form::text('username') }}
</li>
<li>
{{ Form::label('password', 'Password:') }}
{{ Form::password('password') }}
</li>
<li>
{{ Form::submit('Submit', array('class' => 'btn')) }}
</li>
</ul>
{{ Form::close() }}
#if ($errors->any())
<ul>
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
#endif
#stop

There is no users.edit route defined in your routes file. Hence, you are bound to receive that error. There's no users.edit route in your code.
Route::group(['prefix' => 'users', 'before' => 'auth'], function () {
Route::get('dashboard', function () {
$layout = View::make('users.dashboard');
$layout->title = "Dashboard";
$layout->content = View::make('users.dashboard');
return $layout;
});
Route::get('/all/', [ 'as' => 'users.index', 'uses' => 'UserController#all']);
Route::get('/create/', [ 'as' => 'users.create', 'uses' => 'UserController#create']);
Route::get('users/login', [ 'as' => 'users.login', 'uses' => 'UserController#getLogin']);
Route::post('users/login', [ 'as' => 'users.login', 'uses' => 'UserController#postSignin']);
Route::get('users/logout', [ 'as' => 'users.logout', 'uses' => 'UserController#getLogout']);
Route::resource('/','UserController');
});
Add the following route somewhere in your routes file and the error should disappear:
Route::get('/edit/', [ 'as' => 'users.edit', 'uses' => 'UserController#edit']);
Note: You need to replace the controller method name edit to whatever you have defined.
UPDATE 1
That is because, you have 'before' => 'auth' parameter passed in your Route::group() method. Just remove users from edit route and I hope that should do the job for you.
Just A Side note
I presume that you are new in laravel and have less amount of experience with the framework. I would recommend you to go with this only in order to get your hands dirty with the basics. Then once you start gaining the experience and know how things work behind the scenes, you can jump off to the next level.
If I were at your place, I wouldn't have gone with the code that you have provided, instead I would have kept it way too simple for now. My routes.php file would be like this:
/* Routes for users not logged in. */
Route::get('/all', 'UserController#all');
Route::get('/create', 'UserController#create');
Route::get('users/login', 'UserController#getLogin');
Route::post('users/login', 'UserController#postSignin');
Route::get('users/logout', 'UserController#getLogout');
/* Routes for logged in users */
Route::get('/users/dashboard', 'UserSessionsController#dashboard');
Route::get('/users/edit/{id}', 'UserSessionsController#edit');
And then in your view file:
EDIT
Then I would have used the middleware only inside the UserSessionsController file like this:
/**
* Check if the user is authenticated.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth'); // or any other middleware you have.
}
UPDATE 2:
Learn more about the middleware from the following links:
For version: 5
Official Docs
Matt Stauffer On Middleware
For version: 5.1
Official Docs
Youtube Video
For version: 5.2
Official Docs
Hope this helps you out. Happy Coding. Cheers.

in Laravel 8 it's a bit different
Route::get('/users/edit',[UserController::class, 'edit']);

Related

Alert Message not showing in Laravel 5.4

I have got a contact form that is build using Laravel 5.4, parsley.js and Bootstrap 3, it works fine but it does not display a success message at the top of the form on successful delivery of a message or display any errors in case there is an error. Please assist?
Contoller
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#kapsol.com');
});
Session::flash('success', 'Hello $name, Your Form was successfully sent');
return redirect()->route('pages.index');
}
Index.blade.php
<div class="col-sm-4">
{!! Form::open(array('route' => 'index.post', 'class' => 'contact-form', 'data-parsley-validate' => '')) !!}
<div id="success">
<div class="col-sm-12">
#if(Session::has('success'))
<div class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</div>
#endif
#if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</div>
#endif
</div>
</div>
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => '', 'minlength' => '10'))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => '', 'type' => 'email'))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input', 'minlength' => '100')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Route
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
Route::post('/', 'PagesController#postIndex') ->name('index.post');
You are redirecting to the same route your form is posting. I assume that the route that render the view is a GET route and have another name, you should redirect to that route or the route that renders index.blade.php
This is why you have the with() method to chain the route() method on. So rather than use Session:flash(), you can simply add a with() method that flashes that message to the session for the next request:
return redirect()->route('pages.index')->with('success', 'Hello'. $name.', Your Form was successfully sent');
Or rather if your form was originally from the index page, then you simply don't have to remember the name of the route, simply use back() helper method, i.e:
return back()->with('success', 'Hello'. $name.', Your Form was successfully sent');

Laravel a form isn't reset after successful submit

This is my controller:
class GuestbookController extends Controller
{
public function viewAll(Request $request)
{
if ($request->method() === 'POST') {
$this->validate($request, [
'username' => 'required|string|regex:/^[a-zA-Z\d]+$/',
'email' => 'required|string|email',
'homepage' => 'nullable|string|url',
'text' => 'string',
'captcha' => 'required|captcha',
],
[
'captcha.captcha' => 'The captcha is incorrect',
'username.regex' => 'Use English letters and digits only',
]);
$message = new Message();
$message->username = $request->get('username');
$message->email = $request->get('email');
$message->homepage = $request->get('homepage');
$message->text = strip_tags($request->get('text'));
$message->ip = $request->ip();
$message->browser = get_browser($request->header('User-Agent'))->browser;
$message->save();
}
$messages = Message::sortable(['created_at' => 'desc'])->paginate(25);
return view('Guestbook.viewAll', [
'newMessage' => new Message(),
'messages' => $messages
]);
}
}
I am using this plugin. viewAll handles both GET and POST requests, but the problem is that the form isn't reset when I successfully submit data keeping all the previous input values.
I've checked what the server sends and it seems like it sends inputs with last values in them. I've no idea what to do, please help!
View:
#extends('base')
#section('title', 'Guestbook')
#section('baseContent')
{!! BootForm::open(['model' => $newMessage]) !!}
{!! BootForm::text('username') !!}
{!! BootForm::email('email') !!}
{!! BootForm::text('homepage') !!}
{!! BootForm::textarea('text') !!}
{!! captcha_img() !!}
{!! BootForm::text('captcha') !!}
{!! BootForm::submit('Send') !!}
{!! BootForm::close() !!}
#if (count($messages) > 0)
<table class="table table-bordered">
<tr>
<td>#sortablelink('username', 'Username')</td>
<td>#sortablelink('email', 'Email')</td>
<td>Homepage</td>
<td>#sortablelink('created_at', 'Data added')</td>
<td>Message</td>
</tr>
#foreach ($messages as $message)
<tr>
<td>{{ $message->username }}</td>
<td>{{ $message->email }}</td>
<td>{{ $message->homepage }}</td>
<td>{{ $message->created_at }}</td>
<td>{{ $message->message }}</td>
</tr>
#endforeach
</table>
#else
There is nothing to display here.
#endif
{!! $messages->appends(\Request::except('page'))->render() !!}
#endsection
See all code here.
BootForm is compiled like this
<div class="form-group">
<label for="username" class="control-label col-md-2">Username</label>
<div class="col-md-10">
<input type="text" value={{old('username')}} name="username" class="form-control">
</div>
The old('params') is a helper method which keeps the previous inputs in session. And secondly, you have model associated with it.
I hope this helps

Route not going where it's supposed to

I'm getting this error. This error comes in when I hit the delete button
MethodNotAllowedHttpException in RouteCollection.php line 251:
the problem is as far as I know my routes is correct.
My cart.blade.php
#foreach($cartItems as $cartItem)
<tr>
<td>
<img src="{!! asset("product_images/$cartItem->img") !!}" alt="..." class="img-responsive">
</td>
<td>{!! $cartItem->name !!}</td>
<td>{!! $cartItem->qty !!}</td>
<td>R {!! $cartItem->qty * $cartItem->price !!}</td>
<td>
{!! Form::open(array('method' => 'Delete', 'route' => array('deleting', $cartItem->rowId))) !!}
<button class="btn btn-warning">Delete 2</button>
{!! Form::close() !!}
</td>
</tr>
#endforeach
My routes
Route::put('/product/deleting/{id}', [
'uses' => 'OpenController#deleting',
'as' => 'deleting'
]);
my controller
public function deleting($id)
{
echo "string";
}
You are using method DELETE on your form:
Form::open(array('method' => 'Delete',
but defined the method PUT in your route:
Route::put('/product/deleting/{id}', [
Try to change the route to
Route::delete('/product/deleting/{id}', [
Try using 'delete' instead of 'put' in your routes file.

Error message not showing in laravel

I'm using Laravel 5.2 and I'm trying to get the error message to show when I attempt to login with the wrong username and password combination, so when I do that I don't get an error message showing. I'm not sure where I'm going wrong.
My controler
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
class HomeController extends Controller
{
public function login(Request $request)
{
if(Auth::attempt(array('name' => Input::get('name'), 'password' => Input::get('password'))))
{
return redirect()->route('dashboard');
}else{
return redirect()->route('home')
->with('message', 'Your username and password combination is wrong')
->withInput();
}
}
}
my index.blade.php
#if ($errors->any())
{{ implode('', $errors->all('<div>:message</div>')) }}
#endif
<div class="form-wrapper">
<div class="login-header">
<h1>Login</h1>
</div>
<div class="form_input">
{{ Form::open(array('url' => 'admin/')) }}
<div class="form_group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name', '' , array("class" => "form-control")) }}
</div>
<div class="form_group password-section">
{{ Form::label('password', 'Password') }}
{{ Form::password('password', array("class" => "form-control")) }}
</div>
<div class="form_group submit_button">
{{ Form::submit('Submit', array("class" =>"btn btn-info submit", "role" => "button")) }}
</div>
{{ Form::close() }}
</div>
</div>
My routes.php
Route::group(['middleware' => 'web'], function()
{
Route::get('admin/', [
'uses' => 'HomeController#index',
'as' => 'home'
]);
Route::post('/signin', [
'uses' => 'HomeController#login',
'as' => 'Login'
]);
});
use Route::group(['middleware' => 'auth'], function() in routes

Wildcards in Laravel

With the following code, I'm trying to pass a wildcard to my controller but I'm not sure how I can pass the URL dynamically and I'm not sure how to do this without creating a route for each url, which would take forever. Currently, I'm attempting to do it like so:
<a href="{{ route('purchase-get') }}/$item->name">
Here is the rest of the code
<tbody class="text-center">
#foreach (array_chunk($items->all(), 3) as $item_each)
<tr>
#foreach($item_each as $item)
<td>
<a href="{{ route('purchase-get') }}/$item->name">
{{ HTML::image($item->image_url, 'item-image', array('class' => 'item-image-row')) }}
<h4>{{ $item->item }}</h4>
<span class="text-muted">{{ $item->cost }}</span>
</a>
</td>
#endforeach
</tr>
#endforeach
</tbody>
You can link to a wildcard with named routes:
Route::get('/', array('as' => 'index', function()
{
$slug = 'product-1';
return 'link to product';
}));
Then catch the names route with the wildcard slug:
Route::get('products/{slug}', array('as' => 'products', function($slug)
{
$product = Product::where('slug','=',$slug)->first();
return $product;
}));

Resources