I was updating my Laravel 3 app to Laravel 4 when I hit this problem...
Routes I have tried:
Route::get('backend/login', 'backend/UserController#login');
Route::get('backend/login', 'backend.UserController#login');
I had a similar issue just a few hours ago and had to play a little bit with it to have it working.
Routes:
Route::group(array('prefix' => 'admin'), function() {
Route::resource('/', 'admin\DashboardController');
});
In "controllers/admin" i put the DashboardController:
namespace admin;
use Illuminate\Support\Facades\View;
class DashboardController extends \BaseController {
public function index()
{
return View::make('admin/dashboard');
}
}
That did the trick on Laravel 4. Hope you find it useful enough. :)
At the moment, in Laravel 4 Beta 1, you can "only ?" use namespace.
For exemple here in your controller file: app/controllers/backend/UserController.php
<?php namespace Controllers\Backend;
use Illuminate\Routing\Controllers\Controller;
class UserController extends Controller {
// Note extends Controller and not BaseController
// Your stuff
}
?>
So after, in file: app/routes.php :
<?php
Route::get('backend/login', 'Controllers\Backend\UserController#login');
I don't know if is the better way, but working here. Edit & dump-autoload "composer.json" seems not work actualy.
If someone can improve that, he will make my day ! :)
If you are gonna use Laravel 4, perhaps you should take a look of this: You can specify the namespace to be used on a group of routes, as you can see here: http://www.laravel-tricks.com/tricks/route-group-namespacing
So in your sample:
Route::group(array('prefix' => 'backend', 'namespace' => 'backend'), function()
{
Route::get('login', 'UserController#login');
});
It works like a charm :)
I've been using it, and are quite good, it helps you keep your code cleaner and more understandable. Give it a try!
I recommend doing
Route::group(array('prefix' => 'backend'), function() {
// Responds to Request::root() . '/backend/user'
Route::resource('login', 'UserController');
});
see more info here
Laravel 4 nested resource controllers Route::resource('admin/photo', 'PhotoController'); not working
My Admin Controller in app/controllers directory
class AdminController extends BaseController {
/**.
* #return \AdminController
*/
public function __construct()
{
}
}
Now I have a folder named admin in controllers folder i.e app/controllers/admin and I have another controller there named AdminDashboardController.php
class AdminDashboardController extends AdminController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function getIndex()
{
return View::make('admin/dashboard');
}
}
And Lastly My Route.php file
Route::group(array('prefix' => 'admin'), function()
{
# Admin Dashboard
Route::controller('/', 'AdminDashboardController');
});
Hope this helps ..:-)
As explained here, with Laravel 4.1 you can specify the namespace to be used on a group of routes, as you can see here: http://www.laravel-tricks.com/tricks/route-group-namespacing
I've been using it, and are quite good, it helps you keep your code cleaner and more understandable. Give it a try!
You could also put your backend/admin panel in a package..fruit for thought :)
Related
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');
});
Trying to add in user messaging into my website. I have moved the messaging routes to the top of the web.php page, and tried a few other things, but it is just not quite working out. Anyone else have any ideas?
web.php
Auth::routes();
//Messaging
Route::group(['prefix' => 'messaging', 'namespace' => 'Conversation'], function () {
Route::get('/conversations', 'ConversationController#index')->name('conversation.index');
});
conversationcontroller.php
<?php
namespace Hustla\Http\Controllers\Conversation;
use Illuminate\Http\Request;
use Hustla\Http\Controllers\Controller;
class ConversationController extends Controller
{
public function __construct()
{
$this->middleware(['auth']);
}
public function index(Request $request)
{
}
}
"Just not quite working out" is a very vague description of your problem. It would help if you explained what you've tried to do, what you expected to see and what you're actually seeing. It's also worth checking error logs.
If you're trying to view the route in a browser and nothing loads, then perhaps first check the list of compiled routes using php artisan route:list and check that there is an entry for it.
However, looking at your code, it might be a namespace issue you're having. In your route definition, setting the namespace attribute to "Conversation" will tell Laravel to look for the class App\Http\Controllers\Conservation\ConversationController, whereas your actual controller script is under the namespace Hustla\Http\Controllers\Conversation.
In the RouteServiceProvider class, you can specify the "root" namespace for your controller files.
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = 'App\Http\Controllers';
You may want to update this value to "Hustla\Http\Controllers".
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.
I am new to laravel. I am just trying to do some basic stuff using routes and controllers.
I have this controller named testcontroller which looks as below
<?php
class testcontroller extends BaseController{
public function show(){
return 'test';
}
}
I have also defined a route in routes.php as below
Route::get('test', 'testcontroller#show');
when i try to access it using localhost/laravel/public/test it would give the 404 NOT FOUND error, however i have tried rewriting the route in main directory as
Route::get('/', 'testcontroller#show');
and it works all fine, i have even tried and successfully rendered views using this. Can anyone suggest where am i going wrong with the url. Any help will be appreciated.
Thanks :)
Test controller
# app/controllers/TestController.php
class TestController extends BaseController {
public function show()
{
return 'test';
}
}
Routes File
# app/routes.php
Route::get('test', array(
'uses' => 'TestController#show'
));
Route::get('/', array(
'uses' => 'TestController#show'
));
Edit: since that is not working, it is probably to do with your server configuration. Instead of learning how to do that, I'd recommend the use of a VM over WAMP. Also Laravel provides an easy way to use one with Laravel Homestead.
Check out this instructional video on how to do that.
I am putting my Controller called "LoginController" in a folder "login".
class LoginController extends BaseController{
public $restful = true;
//log in function
public function Login(){
// load the login page
return View::make('login.login');
}
}
In the routes, I give this:
Route::get('/',array('uses'=>'login.LoginController#Login'));
Also tried
Route::get('/',array('uses'=>'login\LoginController#Login'));
Route::get('/',array('uses'=>'login\Login#login'));
None of the above seem to work, and give me Class does not exist error.
I am very dumbstruck with this error. Is the way I am accessing the controller in the "uses" correct? Do I need to do any additional things before I can get it to work?
Any help really appreciated!
All you should need is
Route::get('/',array('uses'=>'LoginController#Login'));
Composer need to register this change in routes so dump-autoload composer
php composer.phar dump-autoload
Also if you are using laravel 4, then declaring restful controllers with
public $restful = true;
no longer works.
this happens to me often, just to give a different answer that worked for me
php artisan dump-autoload
Enjoy!
Yeah i had the same issue, i got my answer from https://stackoverflow.com/a/31638718/2821049
Route::group(['namespace' => 'login'], function(
{
// Controllers Within The "App\Http\Controllers\login" Namespace
Route::get('/','LoginController#login');
});
In class you adds :
namespace App\Http\Controllers\folder;
use App\User;
use App\Http\Controllers\Controller;
and in routes you call:
Route::get("admin/login","folder\class#NameFunctionInClass");
Note: folder is the name folder class contains