Shared Hosting- Target class [Controller] does not exist - laravel

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

Related

how group apiResource route and other routes together?

I am using apiResource and other routes. I grouped them like below:
Route::group(['prefix' => 'posts'], function () {
Route::group(['prefix' => '/{post}'], function () {
Route::put('lablabla', [PostController::class, 'lablabla']);
});
Route::apiResource('/', PostController::class, [
'names' => [
'store' => 'create_post',
'update' => 'edit_post',
]
]);
});
all apiResource routes except index and store do not work! How should I group routes?
Your syntax for routing is wrong,
Notes
You will provide a uri for the apiResource (plural)
eg. Route::apiResource('posts', PostController::class);
Your name of resource route is wrong
Get this out https://laravel.com/docs/8.x/controllers#restful-naming-resource-routes
it should be
Route::apiResource('posts', PostController::class)->names([
'store' => 'create_post',
'update' => 'edit_post',
]);
No need of repeating Route::group, you can just write your routes like this
Route::prefix('posts')->group(function () {
Route::put('lablabla', [PostController::class, 'lablabla']);
});
Route::apiResource('posts', PostController::class)->names([
'store' => 'create_post',
'update' => 'edit_post',
]);
Your syntax is incorrect, there is a names method. See the documentation here https://laravel.com/docs/8.x/controllers#restful-naming-resource-routes.

Why is it passing a param when it should not - Laravel Routing

Bizarre issue, lets see some routes:
Route::get('/admin/races', ['as' => 'races.list', 'uses' => 'RacesController#index']);
Route::get('/admin/races/{race}', ['as' => 'races.race', 'uses' => 'RacesController#show']);
Route::get('/admin/races/create', ['as' => 'races.create', 'uses' => 'RacesController#create']);
Route::get('/admin/races/{race}/edit', ['as' => 'races.edit', 'uses' => 'RacesController#edit']);
Seems normal enough, lets see the controller:
class RacesController extends Controller {
public function index() {
return view('admin.races.list');
}
public function show(GameRace $race) {
return view('admin.races.race', [
'race' => $race,
]);
}
public function create() {
return view('admin.races.manage', [
'race' => null,
]);
}
public function edit(GameRace $race) {
return view('admin.races.manage', [
'race' => $race,
]);
}
}
Seems normal enough. The issue is:
When I go to /admin/races/create I get a 404. The reason being is because, exception:
Illuminate\Database\Eloquent\ModelNotFoundException^ {#851
#model: "App\Flare\Models\GameRace"
#ids: array:1 [
0 => "create"
]
#message: "No query results for model [App\Flare\Models\GameRace] new"
#code: 0
#file: "./vendor/laravel/framework/src/Illuminate/Routing/ImplicitRouteBinding.php"
#line: 47
trace: {
.....
Why is calling:
<li>Create Race</li>
Causing Laravel to take the word create and inject it in as a model? No other route that I have, that is similar does this. For context, here's how we create items:
Route::get('/admin/items/create', ['as' => 'items.create', 'uses' => 'ItemsController#create']);
Same concept, just instead of races its items. So how laravel messing this up?
I have run all the cache clears and route clears and everything. Same issue. Even tests are failing on this. No where am I calling this with a param (especially not one called create) so it should not be assuming there is a param.
It's because you have defined Route::get('/admin/races/{race}' .. first, so it'll hit that route regardless of what the value is. Simply moving the create route before the show route will solve your problem.
Route::get('/admin/races', ['as' => 'races.list', 'uses' => 'RacesController#index']);
Route::get('/admin/races/create', ['as' => 'races.create', 'uses' => 'RacesController#create']);
Route::get('/admin/races/{race}', ['as' => 'races.race', 'uses' => 'RacesController#show']);
Route::get('/admin/races/{race}/edit', ['as' => 'races.edit', 'uses' => 'RacesController#edit']);
That said, you can simplify this a lot more with a simple resource-route.
Route::resource('/admin/races', ['as' => 'races.list', 'uses' => 'RacesController'])->only("index", "show", "create", "edit");
An alternative solution to the accepted answer is to specify conditions for the parameter in the parametrised route. For example if {race} needs to be numeric you can do:
Route::get('/admin/races', ['as' => 'races.list', 'uses' => 'RacesController#index']);
Route::get('/admin/races/{race}', ['as' => 'races.race', 'uses' => 'RacesController#show'])->where('race', '\d+');
Route::get('/admin/races/create', ['as' => 'races.create', 'uses' => 'RacesController#create']);
Route::get('/admin/races/{race}/edit', ['as' => 'races.edit', 'uses' => 'RacesController#edit']);
This is useful in the cases where you can't control the order of the routes (e.g. there's a package registering routes that conflict).

Laravel Mail: attach if not null

I am sending emails using the mailable class of Laravel. I want to attach files, if the files exist. So I only want to add the attach parameter if the file exists. I could work with if/else clauses, but I have different files and that would not be clean. A attachIfNotNull function (which does not exist) would be helpful in this case, but maybe there are other clean solutions..
public function build()
{
return $this->view('emails.orders.shipped')
->attach('/path/to/file1', [
'as' => 'name.pdf',
'mime' => 'application/pdf',
]);
}
You can use File::exists() or Storage::exists():
$view = $this->view('emails.orders.shipped');
return \File::exists('/path/to/file1') ? $view
: $view->attach('/path/to/file1', [
'as' => 'name.pdf',
'mime' => 'application/pdf',
]);

Route::match() not working in nested groups structure

I'm creating an API that is available only via POST. I'm planning to have more than one version of the API, so the current one uses v1 as part of the URL.
Now, in case an API call is made via GET, PUT or DELETE I would like to return a Fail response. For this I'm using Route::match(), which works perfectly fine in the code below:
Route::group(['namespace'=>'API', 'prefix' => 'api/v1', 'middleware' => 'api.v1'], function() {
Route::match(['get', 'put', 'delete'], '*', function () {
return Response::json(array(
'status' => 'Fail',
'message' => 'Wrong HTTP verb used for the API call. Please use POST.'
));
});
// User
Route::post('user/create', array('uses' => 'APIv1#createUser'));
Route::post('user/read', array('uses' => 'APIv1#readUser'));
// other calls
// University
Route::post('university/create', array('uses' => 'APIv1#createUniversity'));
Route::post('university/read', array('uses' => 'APIv1#readUniversity'));
// other calls...
});
However, I noticed that I could group the routes even more, to separate the API version and calls to specific entities, like user and university:
Route::group(['namespace'=>'API', 'prefix' => 'api'], function() {
Route::match(['get', 'put', 'delete'], '*', function () {
return Response::json(array(
'status' => 'Fail',
'message' => 'Wrong HTTP verb used for the API call. Please use POST.'
));
});
/**
* v.1
*/
Route::group(['prefix' => 'v1', 'middleware' => 'api.v1'], function() {
// User
Route::group(['prefix' => 'user'], function() {
Route::post('create', array('uses' => 'APIv1#createUser'));
Route::post('read', array('uses' => 'APIv1#readUser'));
});
// University
Route::group(['prefix' => 'university'], function() {
Route::post('create', array('uses' => 'APIv1#createUniversity'));
Route::post('read/synonym', array('uses' => 'APIv1#readUniversity'));
});
});
});
The Route::match() in the code above does not work. When I try to access any API call with e.g. GET, the matching is ignored and I get MethodNotAllowedHttpException.
Can I get the second routes structure to work with Route::match() again? I tried to put it literally everywhere in the groups already. Putting the Route::match() outside of the hole structure and setting path to 'api/v1/*' does dot work either.
If you use the post() function you don't need to deny manualy other verb.
What you can do is to create a listener for the MethodNotAllowedHttpException and display what you want. Or you can also use any() function at the end of your route's group to handle all route that is not defined.

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!

Resources