Controller in subfolder with namespace in laravel-4.1 - laravel

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!

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');

Laravel API call goes through even with session expired

I have a SPA based on Laravel 5.8 and Vue 2.0.
Everything is working fine, a little bit too much to be honest, because if I delete the session and I try to save the content afterward or keep navigating the private pages, every ajax call that I'm doing with Axios is going through without returning any error. Only if I forcefully refresh the page I get the error page I setup but if I don't, I can keep doing everything even if the session no longer exist.
This is my setup.
web.php is where I have the only php route that points to a singlePageController:
Auth::routes();
Route::get('/{any}', 'SinglePageController#index')->where('any', '.*');
Then in the singlePageController I return the view:
class SinglePageController extends Controller
{
public function index() {
return view('app', ['loggedUser' => auth()->user()]);
}
}
Then I have the api.php where I have the API routes. As you can see at the end I have the middleware to make it private. Just to make an example this is the one I use for updating the content:
Route::put('event/update/{slug}', 'EventController#update')->middleware('auth:api');
Then the related controller of that API route:
public function update(Request $request, $slug)
{
$event = Event::where('slug', $slug)->first();
$event->title = $request->input('title');
return new EventResource($event);
}
And in the end this is the Resource I use to define what and how the API data is going to be displayed:
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'title' => $this->title,
'slug' => $this->slug,
'curator' => $this->curator,
'featured_image' => $this->featured_image,
'body' => $this->body,
'date' => $this->date
];
}
So this above is the flow I have. Then when I do an axios call to update the content, I'm doing something like:
axios({
method: 'PUT',
url: '/api/event/update/' + this.$route.params.slug + '?api_token=' + this.isLogged.apiToken,
data: dataToSave,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})
.then((response) => {
this.getNotification('Success: The Event has been saved');
})
.catch((error) => {
this.getNotification('Error: Impossible saving the event');
console.log(error);
})
Thanks in advance for the help
In Laravel routes in api.php ignore the session data.
If you want to authenticate with session data you could move your api routes to web.php and you should see the results you expect.

Laravel Dusk feature tests - getting 403 errors

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

laravel 5.2 make dashboard for admin

I am new in laravel 5.2. I want to make dashboard for admin but i do not understand how to make it. I did copy all controller files in admin folder and also copied view folder in admin folder.
I did try some code which is mention below:-
Route::group(array('namespace'=>'Admin'), function()
{
Route::get('/admin', array('as' => 'admin', 'uses' => 'UserController#index'));
Route::get('/admin/register', array('as' => 'register', 'uses' => 'UserController#register'));
Route::get('/admin/login', array('as' => 'login', 'uses' => 'UserController#login'));
});
but now I want to show all controller files under admin like:-
localhost:8000/admin/users
If you want to use all route of admin on localhost:8000/admin.
You should use route group and add prefix on it, like that
Route::group(['prefix' => 'admin/'], function () {
Route::get('users', function () {
// Matches The "/admin/users" URL
});
Route::get('login', function () {
// Matches The "/admin/login" URL
});
.......
});
Read more at this doc https://laravel.com/docs/5.2/routing#route-groups
I hope this could help you.

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?}'

Resources