Laravel 5.3 Array on routes - php

I'm new to Laravel and still trying to get myself familiar with it's syntax since I originally programmed in Java.
I came across this syntax in one of the tutorials I'm watching.
Route::get('/', [
'uses'=>'ProductController#getIndex',
'as' => 'product.index'
]);
I understand that the ProductController is the controller class, #getIndex is the method (if you will) residing in the ProductController class.
What are uses, as and product.index? I see that they are pairs of keys and values.
Can I modify the uses and as to whatever name I want?
I don't see product.index anywhere in the folder. At first I thought it was a view.
These are the files.
web.php
Route::get('/', [
'uses'=>'ProductController#getIndex',
'as' => 'product.index'
]);
ProductController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class ProductController extends Controller
{
public function getIndex(){
return view('shop.index');
}
}
Product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['imagePath','title','description','price'];
}
Please explain.
I'd appreciate any useful explanation to this.
Thank you.

What you've said is correct. The route is using the ProductController and asking for the getIndex() method. Yup you're free to name the routes how you'd like to, and your methods also.
As the the alias, 'as' is the route name see here (Named Routes).
'product.index'
is the route name.
So you could do...
Route::get('/', 'ProductController#getIndex')->name('product.index');
This would then allow you to use this route for say a redirection.
return redirect()->route('product.index');
It's totally optional to name a route.
Hope that helps!

Related

problems with routes laravel

After data from a form is saved i wanted to get back to the admin page.
I checked the database and the new data was there but I got an error:
"Route [pages.admin] not defined."
My Admin Controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Models\Admin;
class AdminController extends Controller
public function store(Request $request)
{
// Validation code
// Saveing code
return redirect()->route('pages.admin')
->with('success', 'Admins created successfully.');
}
My Page Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controllerpublic
function admin(){
return view('pages.admin');
}
Routes:
Route::get('/admin', 'PagesController#admin');
Route::post('admin_form', 'AdminController#store');
Would appreciate the help.
I looked in online sources but it didn't help
You are confusing the name of a view with the name of a route. Your view has the name pages.admin because there is a admin.blade.php view in the pages folder within the views folder of your application.
For route('pages.admin') to work, you need to assign a name to a route. You may do this by using name() when defining your route.
Route::get('/admin', 'PagesController#admin')->name('pages.admin');
It is a good practise to always name routes. For example: it allows you to just change the url without having to worry about your redirects breaking, since they use the name that hasn't changed.
I found a video and changed my code in the controller to
return redirect('admin');
and it worked.

Target class does not exist. Routing Prefix Laravel 9

I am learning Laravel 9, and I got a problem Target class [Admin\DashboardController] does not exist. when using prefix routing. Is someone can help me to fix it?
this code on routes/web:
Route::prefix('admin')->namespace('Admin')->group(function(){
Route::get('/','DashboardController#index')->name('dashboard');
});
this code on App\Http\Controllers\Admin\DashboardController:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index(Request $request){
return view('pages.admin.dashboard');
}
}
You've specified an incorrect namespace in your route. As per the error message:
Target class [Admin\DashboardController] does not exist
Laravel expects to find a DashboardController in the Admin namespace, however, you've defined your DashboardController with the namespace App\Http\Controllers\Admin.
Update the namespace on your route.
Route::prefix('admin')->namespace('App\Http\Controllers\Admin')->group(function(){
Route::get('/','DashboardController#index')->name('dashboard');
});
If you read the documentation, you will see that you must use another way:
Route::prefix('admin')->namespace('Admin')->group(function(){
Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
});

how to route any user controller in '/' in laravel 8?

I am learning laravel and i saw people creating a route like
Route::get('/user','homeController#fetchSocialLinks');
the homeController has these code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class homeController extends Controller
{
public function fetchSocialLinks()
{
$test= DB::select('SELECT * FROM `social_links`');
return view('/',compact('test'));
}
}
i want to get the $test variable in ('/') i.e. my root address page .
Can any one tell me the right way to do it.
Thanks
When you use the view method, the first argument to pass is a page, i.e you have a blade page named welcome.blade.php, so that function fetchSocialLinks will render a view and you can pass to it a variable like the following code :
return view('welcome',['test'=>$test]);
Some good practices suggest you to name that method "index", concerning the routes you have to know that something changed since Laravel 7, you have to declare your controller like that :
use App\Http\Controllers\homeController;
Route::get('/', [homeController::class, 'fetchSocialLinks']);

Do I need to have Controller.php in all my API subversion?

I am trying to create an API directory in my Laravel project and I'm receiving the following error...
Class 'App\Http\Controllers\Controller' not found
I have tried using use App\Http\Controllers\Controller; on top of my API controllers but no luck.
My API classes are defined like...
class SyncController extends Controller {...}
class TimecardController extends Controller {...}
I'm pretty sure the error is coming from the extends Controller portion.
in App\Http\Controllers\Controller I have Controller.php. One way I have pushed past this is to duplicate the Controller.php into App\Http\Controllers\Api\v2\ and change the namespace of that controller to match where it is located (namespace App\Http\Controllers\Api\v2;)
I don't believe this is correct, as there should be a way to reference the Controller.php from the controllers in the API subdirectory.
../Controllers/Controller.php and API is a subdirectory, ../Controllers/Api/v2/SyncController.php
Any help would be much appreciated.
Thanks
-----------Edit------------
my routes for the api look like so
Route::group(['prefix' => 'api/v2'], function () {
Route::get('sync', 'Api\v2\SyncController#sync')->middleware('auth:api');
Route::post('timecard', 'Api\v2\TimecardController#create')->middleware('auth:api');
});
The Controller class cannot be found because the API controllers are not in the default Laravel controller directory. You need to add the controller class as a use statement. Then the autoloader will be able to find it.
namespace App\Http\Controllers\Api\v2;
use App\Http\Controllers\Controller;
class SyncController extends Controller {...}
And while your at it you might also want to add the auth:api middleware to the entire group. Much safer and efficient.
Route::group(['prefix' => 'api/v2', 'middleware' => 'auth:api', 'namespace' => 'Api\v2'], function () {
Route::get('sync', 'SyncController#sync');
Route::post('timecard', 'TimecardController#create');
});

Laravel 5: Controller subdirectory and Route Group with multiple Controller does not work

Its might be a bug in laravel but not sure, need your suggestions about to resolve this.
The issue is when you use more than one controller under route:group with controller subdirectory except one controller other would be 404s.
Here's my detail code review for senerio:
Routes
#routes.php
#Settings
Route::group(array('prefix' => 'setting'), function() {
#Index
Route::controller('/', 'Setting\IndexController',[
'getIndex'=>'index/setting'
]);
#company detail
Route::controller('company', 'Setting\CompanyController',[
'getInfo'=>'info/company',
'getEdit'=>'update/company'
]);
});
Controllers
IndexController.php
#/app/Http/Controllers/Setting/IndexController.php
namespace App\Http\Controllers\Setting;
use App\Http\Controllers\Controller;
class IndexController extends Controller {
public function getIndex(){
return "setting.index";
}
}
CompanyController.php
namespace App\Http\Controllers\Setting;
use App\Http\Controllers\Controller;
class CompanyController extends Controller {
public function getInfo(){
return ('setting.company.index');
}
public function getEdit(){
return ('setting.company.edit');
}
}
Currently its not working but when you comment one route::controller other will work fine and viceversa.
and also if remove one route:controller and add route like:
Route::get('/', array('as' => 'index/setting', 'uses' => 'Setting\IndexController#getIndex'));
than both will work fine.
But I need to use route:controller for all controllers under route:group.
So, if still there something missing to explain let me know, I will update further in depth.
Any help would be appreciable.

Categories