When I use the command "php artisan route:list" I have this error
[Illuminate\Container\BindingResolutionException]
Unresolvable dependency resolving [Parameter #0 [ <required> $methods ]] in class Illuminate\Routing\Route
The command works nicely when I have
Route::get('/', 'WelcomeController#index');
Route::get('home', 'HomeController#index');
but as soon as i add other routes, I have this exception
I found the problem :
I'm doing IOC in the controller and I inject Illuminate\Routing\Route. As soon as i delete the injection its works.
I have BaseController which extends Controller.
The BaseController is
class BaseController extends Controller {
/**
* #var array Options used by the pages
*/
protected $options;
/**
* #param Route $route
*/
public function __construct(Route $route)
{
$this->options = Option::getAutoloaded();
// Load the options for the named route page
$this->options = array_merge($this->options, Option::getByPage($route->getAction()['as']));
$this->initListPlugins();
}
A controller is
class MediasController extends BaseController {
public function __construct(Route $route)
{
parent::__construct($route);
$this->options = array_merge($this->options, Option::getByPage('media'));
}
Now thre problem is how I fix it :)
Thanx for your help
When ever I see this error I think of typo's, so make sure you recheck everything for those.
Just make sure your Route is linked to the correct Controller#function:
Route::get('medias', ['uses' => 'MediasController#getMediaList', 'as' => 'medias.index'])
Would go to the MediasController in Http/Controllers:
class MediasController extends Controller {
public function getMediaList()
{
return view('medialist');
}
}
Also make sure your controller has a correct return to the view.
And if you're using the
{!! link_to_route('medias.index', 'Media list') !!}
make sure you have the "illuminate/html": "5.0.*#dev" installed through your composer.json aswell.
Related
How to share one method to all controllers with different DI, view and parameters? I need something like this:
public function method(Model $model)
{
$baseData = [
'model' => $model,
'route' => route('$route', [$param => $model]),
];
return view($view);
}
All controllers extend App\Http\Controllers\Controller so you can just place it there
<?php
namespace App\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function method(Model $model, $route, $param, $view)
{
// Declared but not used
$baseData = [
'model' => $model,
'route' => route($route, [$param => $model]),
];
return view($view);
}
}
And use it with $this->method()
For example in HomeController
<?php
namespace App\Http\Controllers;
use App\User;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$user = User::first();
return $this->method($user, 'home', 'user', 'welcome');
}
}
Now accessing domain.tld/home will return the welcome view
If you want to share function to all controller best way will make service in service folder of app.
step to make service:-
1.create service using artisan command
php artisan make:service service_name and define function that to share to all controller in your project.
after making service your have to register this service with provider.make a provider using artisan command.
php artisan make provider:provider_name and you will see 2 function register and boot
register function is used to register your created service and boot for call already register service
register service like this
public function register()
{
$this->app->bind('App\Services\servicename', function( $app ){
return new serviceclassname;
});
}
3.Go config folder ,open app.php where you will get providers array. In this provider you have to define you provider like App\Providers\providerclassname::class,
call this service in controllers like use App\Services\serviceclassname;
public function functionname(serviceclassname serviceobject)
{
serviceobject->functionname();
}
My project is multi language, so in most route I add parameter "locale". But latter I do not need to pass "locale" parameter to my controllers. I try to remove this parameter using forgetParameter() method but when I use this method then I have error "Route not bound".
So what I'm doing wrong.
How to remove "locale" from all routes?
Laravel 5.8
My route file.
Route::group(['prefix' => '{locale}','where' => ['locale' => '[a-zA-Z]{2}']], function() {
Auth::routes([app()->getLocale()]);
Route::get('/', 'HomeController#index')->name('mainPage');
Route::get('/user/{id}','UserController#showUser')->name('getUserProfile')->forgetParameter('locale');
Route::get('/user/{id}/edit','UserController#editUser')->name('editUserProfile')
->middleware('can:editUser,App\User,id')->forgetParameter('locale');
Route::patch('/user/{id}/edit','UserController#updateUser')->name('updateUserProfile')
->middleware('can:updateUser,App\User,id')->forgetParameter('locale');
});
It's not duplicate with this question - Laravel 5.4: How to DO NOT use route parameter in controller
Solution in that question doesn't work in my case. And my question is why "forgotParamter()" throw an error?
Other question is, why I can't use this construction in middleware:
$request->route()->forgetParameter('locale')
I have following error:
Call to a member function is() on null
Thank you.
Try to forget the parameter in an inline middleware in the controller's constructor. Actually, I would use a base controller: 1, get the route value; 2, set to a protected property, so inherited controllers can access to the value; 3, forget the parameter.
<?php
// Localized/Controller.php
namespace App\Http\Controllers\Localized;
use App\Http\Controllers\Controller as ControllerBase;
use Illuminate\Http\Request;
class Controller extends ControllerBase
{
protected string $currentLocale;
public function __construct()
{
$this->middleware(function ($request, $next) {
// If you need to access this later on inherited controllers
$this->currentLocale = $request->route('locale') ?? 'en';
// Other operations, like setting the locale or check if lang is available, etc
$request->route()->forgetParameter('locale');
return $next($request);
});
}
}
<?php
// Localized/UserController.php
namespace App\Http\Controllers\Localized;
use Illuminate\Http\Request;
// Extends the our base controller instead of App\Http\Controllers\Controller
class UserController extends Controller
{
public function show(Request $request)
{
// No need of string $locale arg
dd($this->currentLocale);
}
}
I am using Laravel5.5 and Module package. I have one student module and want to make this as a default for front-end, so committed code of the laravel's default routes/web.php
Here is my student's routes:
<?php
Route::group(['middleware' => 'web', 'namespace' => 'Modules\Student\Http\Controllers'], function() {
/** Frontend routes which does not require authentication
*
*/
Route::get('/', 'FrontEndController#index')->name('frontend.home');
Route::get('/program-search', 'FrontEndController#programs')->name('student.programs');
Route::get('/univeristy-search', 'FrontEndController#univerities')->name('student.universities');
});
And here is my controller code:
<?php
namespace Modules\Student\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Controller;
use Modules\Admin\Http\Models\ProgramCategory;
use Modules\University\Http\Models\Program;
use Modules\Student\Http\Models\Student;
use Modules\University\Http\Models\University;
class FrontEndController extends Controller
{
/**
* Display a listing of the resource.
* #return Response
*/
public function index()
{
return view('student::index');
}
/**
* Show all programs
*/
public function programs(){
$categories = ProgramCategory::orderBy('catagory_name')
->where('status', '=', 'active');
$programs = Program::orderBy('program_name')
->where([
['status', '=', 'active']
]);
$programs->categories = $categories;
return view('student::program_list')
->withPrograms( $programs );
}
public function univerities()
{
return view('student::university_list');
}
}
only first route '/' is working. when I try to access '/program-search' and '/univeristy-search' it throws an error like "No hint path defined for [sutdent]. (View: /var/www/development/unigatenew/Modules/Student/Resources/views/university_list.blade.php)".
What is the wrong I am doing? can anybody help out this?
The mistake was including the same file name inside view. Renaming file name which was included solved the problem.
I have a model config which has the following at the top:
<?php
namespace App;
use DB;
use Illuminate\Database\Eloquent\Model;
class Config extends Model
{
protected $table = 'config';
public function getConfigVariables()
{
$config = DB::table('config')->where('is', '1')->first();
session()->put('name',$config['name']);
session()->put('infoemail',$config['infoemail']);
session()->put('copyrightowner',$config['copyrightowner']);
and I wish to call this in a controller to set up the session so in the route for the top level I set set up
Route::get('/',
[
'uses' => 'ConfigController#ConfigVariables',
'as' => 'home'
]);
The config controller method which does not work is:
public function ConfigVariables()
{
Config::getConfigVariables();
session()->put('thisyear',ReturnCurrentYear());
$footer = "© ".session()->get('thisyear').", ".session()->get('name');
session()->put('footer',$footer);
return view('welcome');
}
but this does not work and I am stuck!
Change
public function getConfigVariables()
to
public static function getConfigVariables()
You might want to read up how object oriented works, basically when you do Config::getConfigVariables(); you are trying to call a static method, without instantiating the class.
A good start would be here, the concept applies everywhere.
I am trying to get started using controllers in Laravel 4 and I am running into some trouble. Here is the basic run down:
I have a controller in the controllers folder called FansController in the file FansController.php:
<php
class FansController extends BaseController {
public $restful = true
public function getindex() {
return View::make('fans.landing');
}
}
Within my views folder, I have a folder called "fans" that controls a view file "landing.blade.php". It contains simple html: <h1>hello</h1>
In my routes.php file, I have a route calling the controller. Here is that code:
Route::get('landing', array('uses' => 'FansController#index'));
When I visit the url: public/fans/landing
I receive a "NotFoundHttpException";
Do you have any ideas what may be going wrong? Thank you for your help.
First off you need to specify what version of Laravel you are running.
If you are running Laravel 4 then:
// routes.php
Route::get('/', array('uses' => 'GuestController#getIndex'));
// GuestController.php
class GuestController extends BaseController {
public function getIndex() {
return 'Hello world.';
}
}
Then run $ composer dump-autoload -o or php composer.phar dump-autoload -o (if your composer is installed locally) on your CLI
In laravel 3, however
// routes.php
Route::get('/', array('uses' => 'GuestController#index'));
// GuestController.php
class GuestController extends BaseController {
public $restful = true;
public function get_index() {
return 'Hello world.';
}
}
Probably you need to change your method name to index:
class FansController extends BaseController {
public $restful = true
public function index() {
return View::make('fans.landing');
}
}