Route in laravel - laravel

I am a newbie to Laravel. I am getting this error. Please advise?
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/Dashboard',function(){
return view('layouts.master');
})

Just add a ; at the end of line 22.
Route::get('/Dashboard',function(){
return view('layouts.master');
}) // <---- HERE
Like this:
Route::get('/Dashboard',function(){
return view('layouts.master');
});

Related

Laravel Seperate folders for Web and API Controllers causing error

Im currently trying to create an API for my laravel project,
I have decided to move my API controllers into a subfolder of Controllers. This is the folder structure:
This is the routes file for the api:
<?php
use Illuminate\Http\Request;
use app\Http\Controllers\APIControllers;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::group(['prefix' => 'v1'], function() {
Route::get('/event_locations', 'EventLocationsAPIController#all');
});
And this is the EventLocationsAPIController:
<?php
namespace App\Http\Controllers\APIControllers;
use App\EventLocation;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class EventLocationAPIController extends Controller
{
public function all()
{
$locations = EventLocation::all();
return response()->json($locations);
}
}
When I send an GET request to /api/v1/event_locations I get the following error
Target class [App\Http\Controllers\EventLocationsAPIController] does not exist.
Any help would be appreciated!
You need to declare the namespace in route group as well.
Route::group(['prefix' => 'v1','namespace'=>'APIControllers'], function() {
Route::get('/event_locations', 'EventLocationAPIController#all');
});
you have given EventLocations plural and the controller name is singular EventLocation change the name of the controller as EventLocationAPIController in the route file.
You have to declare your nameaspace on router's group like this:
Route::namespace('APIControllers')->group(function () {
// Controllers Within The "App\Http\Controllers\APIControllers" Namespace
Route::group(['prefix' => 'v1'], function() {
Route::get('/event_locations', 'EventLocationAPIController#all');
});
});
Check Laravel docs for more info.

Non-static method Illuminate\Routing\Route::middleware() should not be called statically

I have encountered this error in this file : route/api.php
This error refers to line 16
<?php
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
16- Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request::user();
});
Route::post('login', 'Api\AuthController#login');
Problem solved by changing this line
from
use Illuminate\Routing\Route;
to
use Illuminate\Support\Facades\Route;
Since You've not defined in Your question what is error message I can only predict that You're using middleware incorrectly.
Middleware should be assigned to group.
Route::middleware('auth:api')->group(function() {
Router::get('/user', function (Request $request) {
return $request::user();
});
});
or You've to use middleware after defining the route handler:
Route::get('/user', function(Request $request) {
return $request::user();
})->middleware('auth:api');
Because the method middleware is not static method, according to the assigning-middleware-to-routes try it like this:
Route::get('/user', function (Request $request) {
return $request::user();
})->middleware('auth:api');

Assert status in Laravel Dusk

I have a test which verify status. It should be 200
When I run this
public function testBasicTest()
{
$response = $this->json('GET', '/');
$response->assertStatus(200);
}
It pass but when I add url: /clients
always status 404
public function testBasicTest()
{
$response = $this->json('GET', '/clients');
$response->assertStatus(200);
}
Error:
Expected status code 200 but received 404.
Failed asserting that false is true.
change your route.php like below. I added `/clients' route in ti.
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('/clients', function () {
return view('welcome');
});
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});

Method App\Http\Controllers\ProductController::getIndex()() does not exist

web.php file
this is my web.php file using laravel 5.4
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', [
'uses' =>'ProductController#getIndex()',
'as' =>'product.index'
]);
ProductController.php
this is my controller file using laravel5.4
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
//
public function getIndex(){
return view('shop.index');
}
}
How to get rid of this error please help me.
What's wrong with it ?
You shouldn't use () in your route definition. It should be:
Route::get('/', [
'uses' =>'ProductController#getIndex',
'as' =>'product.index'
]);

Laravel 5.2 Authentication - How can I show logged in user's name and the logout link in every page?

Laravel 5.2 Authentication
I created a new authentication scaffolding in Laravel 5.2 using
php artisan make:auth
everything worked perfectly except I get Login/Register links even after logging in when I'm in route
/
but it shows the user's name with a logout link (which is what I want to have in every page) when I'm in route
/home
How can I show logged in user's name and the logout link in every page?
This is because, you are not using the auth middleware on you / route. In your routes.php file the default for this is:
Route::get('/', function () {
return view('welcome');
});
Try moving this closure into the web middleware that was added in when you generated the scaffolding. Your routes.php file should look something like this when complete:
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
// Route::get('/', function () {
// return view('welcome');
// });
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
//
});
Route::group(['middleware' => 'web'], function () {
Route::auth();
// Add this!
Route::get('/', function () {
return view('welcome');
});
Route::get('/home', 'HomeController#index');
});

Resources