Laravel Dusk feature tests - getting 403 errors - laravel

I have a test file in the tests/Feature directory with the following contents:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class UserTest extends TestCase
{
public function testA()
{
$response = $this->actingAs(\App\AdminUser::find(1))
->json('GET', '/users');
$response->assertStatus(200);
}
public function testB()
{
$response = $this->actingAs(\App\AdminUser::find(1))
->json('GET', '/users');
$response->assertStatus(200);
}
}
The problem is that when both tests are executed the assertion in the second one fails with the following error: Expected status code 200 but received 403. - Laravel throws an AccessDeniedHttpException for some reason.
If I comment out either test method the other one works.
Any ideas would be much appreciated.
--- Edit ---
Here's what's in the controller method:
public function index() {
return view('users.index');
}
And the definition in the routes file:
Route::group(['prefix' => 'users', 'as' => 'users.'], function () {
Route::get('/', ['as' => 'index', 'uses' => 'UserController#index']);
Route::get('datatables', ['as' => 'datatables', 'uses' => 'UserController#datatables']);
Route::get('show/{id}', ['as' => 'show', 'uses' => 'UserController#index']);
});
--- Edit 2 ---
Turns out this is an issue with the package I'm using. Further info: https://github.com/JosephSilber/bouncer/issues/306

Related

Shared Hosting- Target class [Controller] does not exist

I have a Laravel project that worked perfectly on my local computer, I uploaded it to the shared server and now I'm having a route error.
Target class [App\Http\Controllers\Web\Back\App\departments\DepartmentController] does not exist.
Route:
Route::group(['namespace' => 'Web\Back\App', 'prefix' => 'app', 'as' => 'app:'], function () {
Route::resource('departments', 'departments\DepartmentController');
Route::resource('projectdepartment', 'departments\ProjectDepartmentController');
});
Controller:
public function index()
{
return Inertia::render('back/app/departments/index', [
'filters' => request()->all('visibility', 'status', 'search'),
'departments' => Department::orderBy('name')->get(),
'users_count' => Department::withCount('users')->orderBy('name')->get(),
]);
}
help Please...
the problem was in my route, on the local server it was case insensitive while on the shared server it is case sensitive.
old
Route::resource('departments', 'departments\DepartmentController');
Route::resource('projectdepartment','departments\ProjectDepartmentController');
Fixed
Route::resource('departments', 'Departments\DepartmentController');
Route::resource('projectdepartment','Departments\ProjectDepartmentController');

nested resources in laravel not worked

I want to have nested resources like that:
Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function ()
{
Route::resource('/articles', 'ArticleController');
Route::group(['prefix' => '/articles', 'as' => 'articles.'], function () {
Route::resource('/types', 'ArticleTypeController');
});
});
But nested route for "article/type" doesn't work, I check my ArticleTypeController outside the "article" route and work.
I really confused, Everybody can help me?
and here is my controller:
class ArticleTypeController extends Controller
{
public function index()
{
$types = ArticleType::all();
return view('manage.articles.types.index')->withtypes($types);
}
}
Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function ()
{
Route::get('articles/types', 'ArticleTypeController#articleTypeMethod');
Route::resource('articles', 'ArticleController');
Route::resource('articles.types', 'ArticleTypeController');
});
for nested resources use articles.types. plural naming is good.
now that manage/articles and manage/articles/1/types will work.
If you want to put a custom route, put it above the resource route if the controller has been used as a resource. see the articles/types [GET] route which maps to ArticleTypeController's articleTypeMethod. now this way http://localhost.com/manage/articles/types should work
here is the 5.1 documentation and it has been removed from documentation of 5.5. but have a look at what Taylor said about it here
it's not recommended on REST to use index function for articles/types, a nested resources index method is used like articles/{id}/types.
for articles/types you need to create a new method.
but if you still want to do it like that. just make it like this
Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function ()
{
Route::get('articles/types', 'ArticleTypeController#index');
Route::resource('articles', 'ArticleController');
Route::resource('articles.types', 'ArticleTypeController', ['except' => ['index']]);
});

Laravel Localization prefix - URl "/" not working without locale

I've did whats written in this post in order to add Localization prefixes to my URLs. However when I visit "/" there is an error: NotFoundHttpException in RouteCollection.php line 161:.
This is my Routesfile web.php:
Route::get('/', ['uses' => 'MainController#showMainPage', 'as' => 'showMainPage']);
Route::group(['prefix' => 'backend'], function () {
Route::get('/login', ['uses' => 'UserController#agentLogin', 'as' => 'agentLogin']);
});
Function:
class MainController extends Controller
{
public function showMainPage()
{
return redirect()->route('/fr');
}
}
localhost:8000/fr and localhost:8000/en are working fine.
How can I redirect / to the fallback locale (/fr)?
You can try make lang param optional:
'prefix' => '{lang?}'

Controller in subfolder with namespace in laravel-4.1

I have the following code:
Route::group(array('namespace' => 'admin'), function() {
Route::group(array('prefix' => 'admin'), function() {
Route::get('group', array('as' => 'adminGroup', 'uses' => 'GroupController#index'));
Route::get('group/index', array('as' => 'adminGroupIndex',
'uses' => 'GroupController#index'));
});
});
and controller
namespace admin;
class GroupController extends \BaseController {
protected $layout = 'dashboard';
public function index()
{
$this->layout->content = \View::make('admin/group/index');
}
}
If I point the URL to:
http://localhost/laravel/public/admin/group/index
works perfectly, but when I point to:
http://localhost/laravel/public/admin/group
does not work. It just redirects to:
http://localhost/laravel/public/user/login
But when I do not use subfolder everything works perfectly!
EDIT: SOLVED
I had started installing laravel administrator and then stopped, because there was not installed an authentication system. So I installed Sentry2 and was configuring management groups. After analyzing a little more settings Laravel Administrator, I realized that it was using the URI 'admin' and also redirected to 'user / login' if I was not authenticated.
Now everything is working perfectly!
You probably have another route filtered with "auth" that is catching that /admin/group URL and sending it to login.
I just reproduced your code here and it works fine for me. For the sake of simplicity I just replaced my routes.php file with this code:
<?php
namespace admin;
class GroupController extends \Controller {
protected $layout = 'dashboard';
public function index()
{
return 'index!';
}
}
\Route::group(array('namespace' => 'admin'), function() {
\Route::group(array('prefix' => 'admin'), function() {
\Route::get('group', array('as' => 'adminGroup', 'uses' => 'GroupController#index'));
\Route::get('group/index', array('as' => 'adminGroupIndex',
'uses' => 'GroupController#index'));
});
});
And both
http://development.consultoriodigital.net/admin/group
http://development.consultoriodigital.net/admin/group/index
Worked fine showing a page with
index!

Laravel - Class Not Found

I am watching a series on tutsplus about building a real application with Laravel. The series was created last year. I assume it was for Laravel 3.
There were a lot of changes I assume and I found out most of them so far and replaced it from the old code. But I can not figure this one out.
In the following code, I get the error ReflectionException - Class questions does not exist even though the class I know is there. I did try composer update and auto-dumb but same output.
I assume there is something wrong with ether of these files:
Code:
QuestionsController.php
class QuestionsController extends BaseController {
public $restful = true;
public function get_index() {
return View::make('questions.index')
->with('title', 'Make it snappy Q&A - Home');
}
}
Route.php:
Route::get('/', array(
'as' => 'home',
'uses' => 'questions#index'
));
Why does it give an error?
that should work
Route::get('/', array(
'as' => 'home',
'uses' => 'QuestionsController#get_index'
));

Resources