url in routes.php not rendering in laravel 4 - php

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.

Related

how to setup routes, controller, and views the proper way in Laravel?

I just started playing around with laravel. First of all, this is the best framework i've came across. Now here is my issue. I am trying to point from routes -> controller -> view
//This is my Controller file
public function index()
{
return View::make('pages', array('name' => 'Taylor'));
}
// This is my Routes File
Route::get('/', 'pagesController#index');
View file => pages.blade.php
This is the error i'm getting.
FatalErrorException in pagesController.php line 19:
Class 'App\Http\Controllers\View' not found
From the error it seems that the problem is that the View class is not found in the current namespace.
Try this way:
//use the View class from the global namespace
return \View::make('pages', array('name' => 'Taylor'));
or import the View class at the beginning of the controller's script:
use View;

Laravel url routes

I have encountered a problem with my routes in Laravel.
Im fairly new to laravel and coding in general, but i had no problems with my routes when working on Mint.
Now i switched to win8 and i can't seem to acces localhost/about.
I get this error:
Class App\Http\Controllers\PagesController does not exist
But my routes.php look like this:
Route::get('about', 'PagesController#about');
Under http>Controllers i have created PagesController.php
yet i get the error that PagesController does not exist.
Can someone please help me, feel like i've tried 100 options, from previous threads.
I have rewrite enabled, i've modified httpd.conf and my .htaccess
Here is my controller,
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controllers;
use Illuminate\Http\Request;
class PageController extends Controller {
public function about() {
$first = "Nichlas";
return view ('pages.about', compact('first'));
}
}

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.

Laravel -> Routed Pagination

I recently started using the Laravel framework and I would like the following (but cannot seem to get it right):
pagination, not the kind which Laravel explains but more the kind of /about.html - /portfolio.html etc.
It seems really difficult to achieve this, I searched for a bit and could not find anything or perhaps im not using the right search terms.
The HomeController serves the layout view that has all the html.
The default route is:
Route::get('/', 'HomeController#show');
And this is the HomeControiler:
class HomeController extends BaseController {
public function show() {
return View::make('layout');
}
}
This isn't pagination, it's just more than one route. Your routes for that would be something like:
Route::get('/', 'HomeController#showIndex');
Route::get('/about', 'HomeController#showAbout');
Route::get('/portfolio', 'HomeController#showPortfolio');
The corresponding controller might be like:
class HomeController extends BaseController {
public function showIndex() {
return View::make('index');
}
public function showAbout() {
return View::make('about');
}
public function showPortfolio() {
return View::make('portfolio');
}
}
You definitely don't put the HTML for different routes all in the same view file (shared navigation should be handled via shared layouts and the #extends blade keyword), and it's best not to use the .html extension when routes are perfectly happy without it.

Route to controller in subfolder not working in Laravel 4

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 :)

Categories