problems with routes laravel - php

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.

Related

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

Laravel 5.6 Custom method to call a view

I have application in laravel.When I try to access a custom method using URL localhost/blog/public/contact/myownview it doesn't give any output. I want to redirect it to view called video.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ContactController extends Controller
{
//My Custom method
public function myownview(){
echo "yest";
//return view('video');
}
}
routes/web.php
Route::get('/contact/myownview','ContactController#myownview');
Try this
Route::get('/my-own-view', 'ContactController#myownview')->name('my-own-view);
and hit the http://localhost:8000/my-own-view, <url>+route name
return view('video');
Make sure in resources/views file has video.blade.php
You need to custom your route.php
Route::get('/blog/public/contact/myownview','ContactController#myownview');

Laravel 5.3 Array on routes

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!

Laravel 5 registering a controller to map all methods

I am new to Laravel 5 coming from CodeIgniter background. I have habit to not play with routes.php. CodeIgniter automatically maps methods like controllerName/MethodName. But in Laravel 5 I am trying to do same by registering a controlller by writing this at top of app/http/sroutes.php:
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
When I run php artisan route:list it show that controller is registered. But when I see URL /public/admin/user/addRole it show addRole method not exist while I have created a method in AdminUserController.
Admin/AdminUserController.php
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getaddRole(){
echo "adding Roles";
}
}
Routes.php
Route::controllers([
'admin/user' => 'Admin\AdminUserController',
]);
<?php namespace App\Http\Controllers\Admin;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AdminUserController extends Controller {
public function getAddRole(){
echo "adding Roles";
}
}
NB: Notice getAddRole() not getaddRole(), use camelCase
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI like this:
public/admin/user/add-role
It's hard to tell because I don't see your controller code but I assume you missed adding a HTTP verb to the method name. Like:
public function getAddRole(){
// ...
}
If you want the method to match any request method, use any:
public function anyAddRole(){
// ...
}

Categories