I do not understand the following problem.
Here are me routes:
Route::get('events', array('as' => 'events' ,'uses' => 'EventController#index'));
Route::get('event/{id}', array('as' => 'event' ,'uses' => 'EventController#view'));
Route::get('event/new_event', array('as'=> 'new_event', 'uses' => 'EventController#newEvent'));
Route::post('event/create', array('uses' => 'EventController#create'));
Route::get('event/{id}/edit', array('as' => 'edit_event', 'uses' => 'EventController#edit'));
Route::post('event/update', array('uses' => 'EventController#update'));
Route::delete('event/delete', array('uses' => 'EventController#destroy'));
I can not create a new event, because when I click on the 'New Event' button, it uses EventController#view instead of EventController#newEvent.
Here is the EventController:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use App\EventModel;
class EventController extends Controller
{
public function index()
{
$events = EventModel::all();
return \View::make('event/index')->with('events', $events);
}
public function view($id)
{
return \View::make('event/view')
->with('event', EventModel::find($id));
}
public function newEvent()
{
dd("dd");
return \View::make('event/create');
}
public function create()
{
$validator = EventModel::validate(Input::all());
if($validator->fails())
{
$messages = $validator->messages();
return redirect()->action('EventController#newEvent')
->withErrors($validator)
->withinput();
}
else
{
EventModel::create(array(
'title'=>Input::get('title'),
'start'=>Input::get('start'),
'end'=>Input::get('end'),
'userID'=>\Auth::user()->id,
));
//Session::flash('message', 'New event has been created!');
flash()->overlay('New event has been created!', 'Success');
return redirect()->back();
}
}
public function edit($id)
{
return \View::make('event/edit')
->with('event', EventModel::find($id));
}
public function update()
{
$event = EventModel::find(Input::get('event_id'));
$validator = EventModel::validate(Input::all());
if($validator->fails())
{
$messages = $validator->messages();
return redirect()->back()
->withErrors($validator)
->withinput();
}
else
{
$event->title = Input::get('title');
$event->start = Input::get('start');
$event->end = Input::get('end');
$event->save();
//Session::flash('message', 'Successfully updated!');
flash()->overlay('Event has been sucessfully updated!', 'Success');
return redirect()->back();
}
}
public function destroy()
{
$id = Input::get('event_id');
dd("$id");
}
}
Why does this problem occur?
You have to sort your routes because laravel checks the order of the routes.
Try:
Route::get('events', array('as' => 'events' ,'uses' => 'EventController#index'));
Route::get('event/new_event', array('as'=> 'new_event', 'uses' => 'EventController#newEvent'));
Route::post('event/create', array('uses' => 'EventController#create'));
Route::post('event/update', array('uses' => 'EventController#update'));
Route::delete('event/delete', array('uses' => 'EventController#destroy'));
Route::get('event/{id}', array('as' => 'event' ,'uses' => 'EventController#view'));
Route::get('event/{id}/edit', array('as' => 'edit_event', 'uses' => 'EventController#edit'));
Laravel route checks in the order they were defined.
event/new_event and event/{id} both have same route structure and so it is going to view action.
Change the order -
Route::get('event/new_event', array('as'=> 'new_event', 'uses' => 'EventController#newEvent'));
Route::get('event/{id}', array('as' => 'event' ,'uses' => 'EventController#view'));
Related
I'm having a strange issue with my laravel app .
I have a route defined as :
web.php
Route::get('/', ['as' => '/', 'uses' => 'LoginsController#getLogin']);
Route::post('/login', ['as' => 'login', 'uses' => 'LoginsController#postLogin']);
Route::group(['middleware' => ['authenticate', 'roles']], function (){
Route::get('/logout', ['as' => 'logout', 'uses' => 'LoginsController#getLogout']);
Route::get('/dashboard','DashboardController#dashboard')->name('dashboard');
});
In a controller , i'm trying to redirect to this route
LoginController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LoginsController extends Controller
{
use AuthenticatesUsers;
protected $username = 'username';
protected $redirectTo = '/';
protected $guard = 'web';
public function getLogin()
{
if (Auth::guard('web'))
{
return redirect()->route('dashboard');
}
return view('login');
}
public function postLogin(Request $request)
{
$auth = Auth::guard('web')->attempt([
'username' => $request->username,
'password' => $request->password,
'active' => 1]);
if ($auth)
{
return redirect()->route('dashboard');
}
return redirect()->route('/');
}
public function getLogout()
{
Auth::guard('web')->logout();
return redirect()->route('/');
}
}
Where I am typing http://localhost:8000 in address bar of browser. I see.
The following route works fine and the method can loop through the items:
http://localhost/library/api/books
$app->get('/api/books', 'Book:getBooks');
The class:
class Book {
:
:
public function __construct($container) {
$this->container = $container;
}
public function getBooks($request, $response) {
:
:
echo '{"book": ' . json_encode($books) . '}';
}
public function getBook($id) {
echo json_encode($id);
}
}
Calling the method with route pattern identified by 'id' as follows, returns nothing (empty $id):
http://localhost/library/api/books/10
$app->get('/api/books/{id}', 'Book:getBook');
It seems like 'id' won't pass.
How to handle correctly route pattern by identifier?
As I said in the comments, please let us know what the dev console says for instance in chrome under the category console and network.
I am not sure why you choose to create your routes like that, but I would create them following way (which also looks more tidy) :
Route::group(['prefix' => 'book'], function ()
{
Route::get('/', ['as' => 'index', 'uses' => 'BookController#index']);
Route::get('new', ['as' => 'new', 'uses' => 'BookController#new']);
Route::get('show/{bookID}', ['as' => 'show', 'uses' => 'BookController#show']);
Route::get('edit/{bookID}', ['as' => 'edit', 'uses' => 'BookController#edit']);
Route::post('create', ['as' => 'create', 'uses' => 'BookController#create']);
Route::post('update', ['as' => 'update', 'uses' => 'BookController#update']);
Route::delete('destroy/{deviceID}', ['as' => 'destroy', 'uses' => 'BookController#destroy']);
});
The BookController would look like this then:
class BookController extends Controller
{
// this shows all books and adds a pagination of 15 items, which you can easily increase or decrease
public function index()
{
$books = DB::table('books')->paginate(15);
return view('books.index', compact('books');
}
public function new()
{
$book = new Book;
return view('books.new', [
'books' => $books,
'type' => 'new'
]);
}
public function create(Request $request)
{
$this->validate($request, Book::$rules); // I put the rules inside of the Book Model, but you could just add them here aswell
$data = $request->all();
$book = new Book();
$book->fill($data);
if($book->save())
{
return redirect()->route('new')->with('success', 'success.');
}
else
{
return redirect()->route('new')->with('error', 'Error.')->withInput();
}
public function edit(Request $request, $bookID = 0)
{
$books = Book::all();
$newBook = new Book;
$book = Book::find($bookID);
if(is_null($book))
{
$books = Device::paginate(10); // paginate if you like to
return view('books.index', [
'books' => $books,
'errorNoBook' => 'No BOok'
]);
}
else
{
$bookID = $book->id;
}
return view('books.edit', [
'allBooks' => $allBooks,
'new' => $new,
'book' => $book,
]);
}
}
The following is a simple working solution that I found:
:
:
public function getBook($request, $response) {
$route = $request->getAttribute('route'); // route object
$id = $route->getArgument('id'); // route object identifier
$book = $this->db->table('books')->where('id', $id)->first();
echo json_encode($book);
}
I have this protected route to be accessed only when the user is authenticated:
Route::get('/checkout', [
'middleware' => 'auth',
'uses' => 'Front#checkout'
]);
and the other routes are:
// Authentication routes...
Route::get('auth/login', 'Front#login');
Route::post('auth/login', 'Front#authenticate');
Route::get('auth/logout', 'Front#logout');
// Registration routes...
Route::post('/register', 'Front#register');
And my controller is:
<?php
namespace App\Http\Controllers;
use Request;
use Redirect;
use App\User;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
class Front extends Controller {
public function register() {
if (Request::isMethod('post')) {
User::create([
'name' => Request::get('name'),
'email' => Request::get('email'),
'password' => bcrypt(Request::get('password')),
]);
}
return Redirect::away('auth/login');
}
public function authenticate() {
if (Auth::attempt(['email' => Request::get('email'), 'password' => Request::get('password')])) {
return redirect()->intended('/checkout');
} else {
return view('auth/loginerror', array('title' => 'Welcome', 'description' => '', 'page' => 'home'));
}
}
public function login() {
return view('auth/login', array('page' => 'home'));
}
public function checkout() {
return view('contactme', array('page' => 'home'));
}
public function logout() {
Auth::logout();
return Redirect::away('auth/login');
}
}
how is this error in the route?
NotFoundHttpException in RouteCollection.php line 161:
I tried to create a login, after I tried successfully identified,
return "Success Login";
but when I replace with
return Redirect::to('dashboard');
always returned to Login page
can you help me ? what's wrong with my code...
here is my code :
route.php
Route::get('login',array('as' => 'login', 'uses' => 'AuthController#getLogin'))->before('guest');
Route::post('login',array('uses' => 'AuthController#postLogin'))->before('csrf');
Route::group(array('before' => 'auth'), function(){
Route::get('dashboard', array('as' => 'panel', 'uses' => 'DashboardController#view_dashboard'));
});
AuthController.php
class AuthController extends Controller {
public function getLogin(){
return View::make('users.login');
}
public function postLogin(){
$rules = array('username' => 'required', 'password' => 'required');
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()){
return Redirect::route('login')->withErrors($validator);
}
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password')
), false);
if(!$auth){
return Redirect::route('login')->withErrors(array(
'Maaf anda bukan sebagai admin..'
));
}
//return "Success";
return Redirect::to('dashboard');
}
}
DashboardController.php
class DashboardController extends Controller {
public function view_dashboard(){
return View::make('dashboard.view_home_admin');
}
}
view_home_admin.blade.php
<h1>Welcome <small>{{ ucwords(Auth::user()->username) }}</small></h1>
I'm getting this TokenMismatchException with Laravel 4.2.
TokenMismatchException will show up when I trying to post request.
For example Login Page.
If I submit that form TokenMismatchException will show up.
Is there any way I can validate all post request submitted ?
Here's the error :
Route::filter('csrf', function()
{
if (Session::token() != Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});
Here's my code :
route.php
Route::get('login',array('as' => 'login', 'uses' => 'AuthController#getLogin'))->before('guest');
Route::post('login',array('uses' => 'AuthController#postLogin'))->before('csrf');
Route::group(array('before' => 'auth'), function(){
Route::get('dashboard', array('as' => 'panel', 'uses' => 'DashboardController#view_dashboard'));
});
AuthController.php
class AuthController extends Controller {
public function getLogin(){
return View::make('users.login');
}
public function postLogin(){
$rules = array('username' => 'required', 'password' => 'required');
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()){
return Redirect::route('login')->withErrors($validator);
}
$auth = Auth::attempt(array(
'username' => Input::get('username'),
'password' => Input::get('password')
), false);
if(!$auth){
return Redirect::route('login')->withErrors(array(
'Maaf anda bukan sebagai admin..'
));
}
//return "Success";
return Redirect::to('dashboard');
}
}
DashboardController.php
class DashboardController extends Controller {
public function view_dashboard(){
return View::make('dashboard.view_home_admin');
}
}
seems like you don't have hidden csrf field in your form.
try to add this in your form
{{ Form::token() }}