Cesar Henrique Damascena Cesar Henrique Damascena - 4 months ago 41
PHP Question

Laravel 5.2 routes not working properly

i'm new to Laravel and I'm having some issues with the routes class

Here is my routes.php

Route::get('/', function () {
return view('welcome');
});

Route::auth();

Route::get('/home', 'HomeController@index');

Route::resource('/dashboard', 'DashboardController');
Route::resource('/dashboard/users', 'DashboardUsersController');
Route::resource('/dashboard/posts', 'DashboardPostsController');


All of the above routes are working except

app.dev/dashboard/users/index
app.dev/dashboard/posts/index


every route that i create like

Route::get('dashboard/users/import', function (){
return 'Importing a user';
});


or

Route::get('dashboard/users', 'DashboardUsersController@getUsers');


I already tried some suggestions that I've found here, like cleaning the cache, view and route but none worked, my project folder's permission is 777

Here's my DashboardUsersController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class DashboardUsersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.users.index');
}


public function getUsers()
{
return view('admin.users.index');
}


/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.users.index');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
return view('admin.users.index');
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

Answer

For simplicity use Route::get for normal methods in controller and Route::post for methods that are receiving $_POST data from form.

Second parameter of Route::get / Route::post method is ControllerName@MethodName. Try to change it accordingly.

Try not to forget namespace.

Edit: When using Route::resource you are actually generating all possible RESTful routes for your controller. You can not make that more than once. That is the reason why changing Route::resource to Route::get did work.

You can find more information here: https://laravel.com/docs/5.2/controllers#restful-resource-controllers

Comments