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

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

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.

Call to undefined method Illuminate\Support\Facades\App::index()

I'm new to learning Laravel but I'm having trouble routing to controller, I have a controller named "App" and I have a function named index in it, it says it can't find it in "App" controller even though I set it in the route
Error
Error
Call to undefined method Illuminate\Support\Facades\App::index()
http://localhost:8000/anasayfa
App.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class App extends Controller
{
public function index(){
return "anasayfa";
}
}
web.php
Route::get('/anasayfa', 'App#index');
What is the reason for this error?
A class with the name App already exists in Laravel, defined in namespace Illuminate\Support\Facades\App
if you want to use your class make sure to add
use App\Http\Controllers\App
in your web.php
It is recommended to use a different name. You should follow the conventions and name it AppController.
I'm solved.
I deleted the controller named "App" and created a controller named "AppController". But this caused a new error, Laravel could not find class "AppController". For this I updated web.php as follows;
use App\Http\Controllers\AppController;
Route::get('/anasayfa', [AppController::class, 'index']);

How to pass a model instance to all controllers and views based on a route parameter

I am working on a Laravel control panel project where we should be able to toggle from one site to another and get the detail of the site based on the ID passed in the route.
In itself this is quiet easy to do but as I will have several controllers using this technique it means for each controller and each controller instance I will have collect the site instance and it does not look very user friendly due to the many repetitions.
Here is what I have:
Route:
Route::get(
'cp/site/{website}/modules/feeds',
'App\Http\Controllers\Modules_sites\Feeds\FeedController#index'
)->name('module_site.feeds.index');
Model:
class Website extends Model
{
use HasFactory;
protected $primaryKey ='site_id';
}
The database is simple with an id (site_id) and name
Controller:
public function index(Website $website)
{
dd($website -> name);
}
The above is working fine but I am going to end with dozens of methods across multiple controllers doing the same thing, and what if changes are required.
I have looked at the ID of using the AppServiceProvider to create the Website instance and then pass it to the controllers and views but I can't do this as the route is not defined at this stage and I only seem to be able to pass this to the view.
Essentially, I am looking to create something similar to the auth()->user() method that is available from controllers and routes without the needs to pass it to each controller.
Is this possible?
Perhaps you could use middleware to set this value? Something like this to put it in the session globally:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class CheckWebsite
{
public function handle(Request $request, Closure $next): mixed
{
$request->session()->put("website", $request->route("website"));
return $next($request);
}
}
Or this on a per-controller basis:
<?php
namespace App\Http\Controllers\Modules_sites\Feeds;
use App\Http\Controllers\Controller;
use Closure;
use Illuminate\Http\Request;
class FeedController extends Controller
{
public function __construct()
{
$this->middleware(function (Request $request, Closure $next) {
$this->website = $request->route("website");
return $next($request);
});
}
public function index()
{
dd($this->website->name);
}
}
Also worth mentioning that routes are not defined like that in Laravel 8 any longer. It should look like this:
Route::get(
'cp/site/{website}/modules/feeds',
[FeedController::class, 'index']
)->name('module_site.feeds.index');
With an appropriate import for the controller class.
as you primary key is not id so it will not work automatically you need to tell laravel to search by column name
code will be
Route::get('cp/site/{website:site_id}/modules/feeds', 'App\Http\Controllers\Modules_sites\Feeds\FeedController#index')->name('module_site.feeds.index');
you need to use {website:site_id}
ref link https://laravel.com/docs/8.x/routing#customizing-the-default-key-name

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

Redirect action to controller in different namespace

I have middleware that uses a redirect to call a controller which then displays a view.
public function handle($request, Closure $next)
{
redirect()->action('Full\Namespace\To\Controller\ErrorController#fourOhThree');
}
I also have this as a route. When I follow the route the view is displayed fine. When I try and redirect using action and pass the namespace of my controller, laravel tries to find the controller in the base app. I get error
Action App\Http\Controllers\Full\Namespace\To\Controller\ErrorController#fourOhThree not defined.
When the controller is located at
App\Vendor\Myname\Mypackagename\Controllers\ErrorController#fourOhThree
I have namespaced my controller correctly as far as I can tell as it matches the other namespaced controllers in this directory. This is the only controller I am trying to call from an action.
ErrorController.php within App\Vendor\Myname\Mypackagename\Controllers
namespace Full\Namespace\To\Controller;
use App\Http\Controllers\Controller;
class ErrorController extends Controller
{
public function fourOhThree()
{
return view('...');
}
}
I think I am doing something wrong in how I am passing the namespaced controller to the action method.
Try adding a '\' in front of the qualified name.
action('\Full\Namespace\To\Controller\ErrorController#fourOhThree')

Categories