Laravel Undefined offset: 1 - php

I want to add a authentication when guest try to access the home page. I get the error when I try to access the home page.
Laravel Version : Laravel 5
The routes.php error version:
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::get('/', ['middleware' => 'auth', function()
{
Route::get('/', 'HomeController#index');
Route::get('pages/{id}', 'PagesController#show');
Route::post('comment/store', 'CommentsController#store');
}]);
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'], function()
{
Route::get('/', 'AdminHomeController#index');
Route::resource('pages', 'PagesController');
Route::resource('comments', 'CommentsController');
});
Error Log
at HandleExceptions->handleError('8', 'Undefined offset: 1', 'G:\wamp\www\Laravel5\vendor\compiled.php', '7377', array('request' => object(Request), 'this' => object(Route))) in compiled.php line 7377
The correct origional version
Route::get('/', 'HomeController#index');
Route::get('pages/{id}', 'PagesController#show');
Route::post('comment/store', 'CommentsController#store');
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'auth'], function()
{
Route::get('/', 'AdminHomeController#index');
Route::resource('pages', 'PagesController');
Route::resource('comments', 'CommentsController');
});
Update:
I know solve the problem but don't know why? You can see the last four lines I declared a route group with authentication. And when I test that part, I had logged in. Now I log out the /admin prefix and I can see the login page when I try to access the root url.
So my idea is write a guestAuth middleware for users and a adminAuth middleware to administrators. Do you have better ideas?

Try with -
Route::group(['middleware' => 'auth'], function()
{
Route::get('/', 'HomeController#index');
Route::get('pages/{id}', 'PagesController#show');
Route::post('comment/store', 'CommentsController#store');
});
Check here

Is it this line of code?
Route::get('/', ['middleware' => 'auth', function()
{
Route::get('/', 'HomeController#index');
Route::get('pages/{id}', 'PagesController#show');
Route::post('comment/store', 'CommentsController#store');
}]);
Should it be?
Route::get('/', ['middleware' => 'auth'], function()
{
Route::get('/', 'HomeController#index');
Route::get('pages/{id}', 'PagesController#show');
Route::post('comment/store', 'CommentsController#store');
});

Related

Trying to send data with axios got the error 404 not found laravel/vue

I am trying to save data from Vue.js to a Laravel controller with axios, but I am getting this 404 not fund error when I try with postman and on console.
After I triend to change the axios path to:
axios.post('storeInvoice' , {item: this.selectedItem , invoice_id:this.invoice_id}).then(response=>{
console.log(response);
})
I got 405 Method not allowed
web.php
Route::post('storeInvoice', [InvoiceController::class , 'storeInvoice']);
Items.vue
axios.post('http://127.0.0.1:8000/storeInvoice' , {item: this.selectedItem ,
invoice_id:this.invoice_id}).then(response=>{
console.log(response);
})
InvoiceController.php
public function storeInvoice(Request $request){
dd('added'); //just for testing purpose
}
full web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\ItemController;
use App\Http\Controllers\InvoiceController;
use App\Http\Controllers\CompanyDetailsController;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Auth::routes();
Route::get('/home', 'App\Http\Controllers\HomeController#index')->name('home')->middleware('auth');
Route::group(['middleware' => 'auth'], function () {
Route::resource('categories', CategoryController::class);
Route::resource('customers', CustomerController::class);
Route::resource('items', ItemController::class);
Route::resource('invoices', InvoiceController::class);
Route::get('invoice/export', [InvoiceController::class , 'export'])->name('exportInvoice');
Route::get('item/export', [ItemController::class ,'export'])->name('exportItem');
Route::get('customers/export', [CustomerController::class ,'export'])->name('exportCustomer');
Route::post('storeInvoice', [App\Http\Controllers\InvoiceController::class , 'storeInvoice']);
});
Route::group(['middleware' => 'auth'], function () {
Route::resource('user', 'App\Http\Controllers\UserController', ['except' => ['show']]);
Route::get('profile', ['as' => 'profile.edit', 'uses' => 'App\Http\Controllers\ProfileController#edit']);
Route::put('profile', ['as' => 'profile.update', 'uses' => 'App\Http\Controllers\ProfileController#update']);
Route::put('profile/password', ['as' => 'profile.password', 'uses' => 'App\Http\Controllers\ProfileController#password']);
});

Laravel Routes and Auth

In my web.php has
Route::post('/user/logout', function () {
Auth::logout();
return redirect()->route('login');
})->name('user-logout');
Auth::routes();
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath']
], function(){
Route::get('/','SiteController#index')->name('site.index');
Route::group(['prefix' => 'admin'], function () {
Route::get('/', 'AdminController#index')->name('admin.index');
Route::prefix('users')->group(function () {
Route::get('/', 'AdminControllers\UserController#index')->name('admin.users.index');
Route::get('/create', 'AdminControllers\UserController#create')->name('admin.users.create');
Route::post('/store', 'AdminControllers\UserController#store')->name('admin.users.store');
Route::get('/show/{id}', 'AdminControllers\UserController#show')->name('admin.users.show');
Route::get('/edit/{id}', 'AdminControllers\UserController#edit')->name('admin.users.edit');
Route::put('/update/{id}', 'AdminControllers\UserController#update')->name('admin.users.update');
Route::delete('/destroy/{id}', 'AdminControllers\UserController#destroy')->name('admin.users.destroy');
});
});
});
when I try to login with this url
http://dev.practice.uz/
or with this url
http://dev.practice.uz/admin
I go to the same login blade and the same route, I should do separately route and login.blade , Can you give Advise me??

Laravel 5 route same urls login or not

I am updating a site from Laravel 4 to 5. In L4 I had this set up:
if(Sentry::check()){
Route::get('/', array('as' => 'school.home.index', 'uses' => 'school\AuthSchoolController#index'));
else{
Route::get('/', 'school\SchoolController#index');
}
Note the same url but different controllers depending on login or not.
With L5 I cannot use the middleware tried this:
Route::get('/', 'SchoolController#index');
Route::group(['middleware' => 'auth'], function()
{
Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController#index'));
});
But this just passes over the first and goes to the group, where it gets redirected to the login page and to the admin if logged in.
So I think I need an if/else equivalent in the route based on login but Auth::user() for doesn't seem to work:
if(Auth::check()){
Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController#index'));
}
else{
Route::get('/', 'SchoolController#index');
}
Route::get('/', function()
{
if( Auth::check() ) {
return app()->make('App\Http\Controllers\SchoolController')->callAction('index', []);
} else {
return app()->make('App\Http\Controllers\AuthSchoolController')->callAction('index', []);
}
});
Try reordering the routes like so:
Route::group(['middleware' => 'auth'], function()
{
Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController#index'));
});
Route::get('/', 'SchoolController#index');

Laravel Middleware except with Route::group

I'm trying to create a group Route for the admin section and apply the middleware to all paths except for login and logout.
What I have so far is:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'authAdmin'], function() {
Route::resource('page', 'PageController');
Route::resource('article', 'ArticleController');
Route::resource('gallery', 'GalleryController');
Route::resource('user', 'UserController');
// ...
});
How would I declare exceptions for the middleware with the above setup?
Simply nest groups and then you can exclude specific routes:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
Route::get('login', 'AuthController#login');
Route::get('logout', 'AuthController#logout');
Route::group(['middleware' => 'authAdmin'], function(){
Route::resource('page', 'PageController');
Route::resource('article', 'ArticleController');
Route::resource('gallery', 'GalleryController');
Route::resource('user', 'UserController');
// ...
});
});
You can also use laravel's withoutMiddleware method as below;
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'authAdmin'], function() {
Route::resource('page', 'PageController');
Route::resource('article', 'ArticleController');
Route::resource('gallery', 'GalleryController');
Route::resource('user', 'UserController');
Route::get('login', 'AuthController#login')->withoutMiddleware([AuthAdminMiddleware::class]);
Route::get('logout', 'AuthController#logout')->withoutMiddleware([AuthAdminMiddleware::class]);
});

How to use Redirect::intended in laravel?

I want the route to bypass authentication filter after successfully login. So I use Redirect::intended. But it makes problem. Here is my code
In LoginController#doLogin
if (Auth::attempt($credentials)) {
return Redirect::intended("home.index");
}
In Routes
Route::group(array('before' => 'auth'), function() {
Route::resource('home', 'HomeController');
Route::get('/', 'HomeController');
});
My routes
If I put the the home resource route without the auth filter, then it will work. (Not redirect to login route.).That code is given below
Route::resource('home', 'HomeController');
Route::group(array('before' => 'auth'), function() {
Route::get('/', 'HomeController');
}); /* No Problem with this code */
But I want to work with auth filter.I'm using laravel 4.
Please help me...
Currently I use this:
in filter.php
Route::filter('sentry', function() {
if (!Sentry::check())
{
return Redirect::route('authLogin');
}
});
so in route.php
Route::get('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login'));
Route::post('auth/login/', array('as' => 'authLogin', 'uses' => 'AuthController#login'));
Route::group(array('before' => 'sentry'), function() {
Route::get('user/', array('as' => 'user', 'uses' => 'UserController#index'));
Route::get('user/index', array('as' => 'userIndex', 'uses' => 'UserController#index'));
});
I preffer named routes.

Categories