<?php
class TasksController extends BaseController {
public function index()
{
return 'show all tasks.';
}
}
?>
That's what I did very simple. But when I go to localhost:8888/tasks
it doesn't show the result it shows Whoops! Something went wrong.
What could possibly be the problem, such a simple code? By the way I just set up Laravel recently.
Controllers don't do anything until you point a route at them, via app/routes.php.
http://laravel.com/docs/routing
Adding:
Route::get('tasks', 'TasksController#index');
is likely to do the trick.
Related
I am familiar with Laravel 4 routes, but I am experiencing some problem with Laravel 5.
I code route.php as:
Route::get('/','HomeController#index');
and my HomeController.php is the following:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return View::make('index');
}
}
The output page displays as:
Whoops,looks like something went wrong.
The Route annotation file seems to be perfect.
The same case occurs for folder routing too!!
Please help me out.
First of all you should follow the instructions from James Njuguna in a comment to your question. Withoug debugging we can only guess whats going wrong.
In your case, most likely your error is, that the line
return View::make('index');
is causing an exception, because class App\Http\Controllers\View is not found. In this file a namespace is used, so you have to reference the root namespace like:
return \View::make('index');
OR you use a helper function
return view('index');
This function is documentated at http://laravel.com/docs/5.0/helpers#miscellaneous
If that's still failing... maybe you don't have an index.php or index.blade.php in your resources/views folder.
All, what #shock_gone_wild and #JamesNjuguna said is true. The reason of an error occurring is that you do not use namespaces when you call View.
For testing you can simply return text from a controller like this:
public function index()
{
return 'test'
}
and when it returns a result you can see what was the reason for the error and than you can change it with view global function, like #JamesNjuguna said.
Try this
public function index()
{
return view('home');
}
In laravel 5 the view class is not illuminated using a capital letter at the beginning
Hi guys I hope someone can help me, I'm using laravel 4 and I have a REST controller like this
Route::controller('example', 'SomeClass');
class SomeClass extends BaseController {
public function getThisIsATest() {
return 'Hello World';
}
}
but when I try to enter the URI localhost/example/this-is-a-test
I get a 'Controller method not found.'
I'm thinking it's because the two consecutive uppercase letters in the 'getThisIsATest'
Does someone know how to fix this issue??
Two ways (without changing the URL)...
1. Use an additional route
Add an implicit route for the URL before the controller route:
Route::get('example/this-is-a-test', 'SomeClass#getThisIsATest');
Route::controller('example', 'SomeClass');
2. Change your method name to snake_case
You can change the method name in your controller to snake case and everything will work as excepted. (It doesn't look very pretty though)
public function get_this_is_a_test() {
return 'Hello World';
}
Actually it even works to only write the "problematic" part in snake case: getThisIsA_test. However this looks even more weird ;)
Try this:
Route::resource('example/this-is-a-test', 'SomeClassController#getThisIsATest');
OR:
Route::get('example/this-is-a-test', 'SomeClassController#getThisIsATest');
See, if that solves your problem & Do visit Laravel Controller Docs for more information
Add {{ URL::to('example/this-is-a-test'); }} and you're good to go
In your app/routes.php try defining your route as follows:
Route::controller("example", "testController");
And rename your controller class to
class testController extends BaseController {
public function index(){
// Just have it here to be safe.
}
public function getTest() {
return 'Hello World';
}
}
Now try navigating to localhost/example/test. Let me know if that works, I've never used Route::controller() before, so it's new to me as well.
Edit
As the above doesn't work for this example, try navigating to localhost/example/this-is-atest without changing your controller/route.
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.
In the Layouts folder, I have a layout called signup.blade.php
In my controller, I'm assigning a layout to it like so:
protected $layout = 'layouts.signup';
In a separate folder, named "signup" I have a file called "signup1.blade.php" It contains your typical blade template stuff. It's a section called "content". Before the code I have
#section('content')
and it's got #stop at the end.
My controller looks like this:
public function SignUp()
{
$this->layout->content = View::make('signup.signup1');
}
The frustrating part is that this is working with another layout and controller. I've double checked they're the same, and this one does not work. Any suggestions would be greatly appreciated. Thank you.
So, assuming this controller extends BaseController (it must for $layout to work), the code execution sets $this->layout to View::make($this->layout).
Your error seems to show that $this->layout is not getting set to a View object correctly.
Try to run this and see if $this->layout is an object/class, and if so, what class it is.
public function SignUp()
{
echo gettype($this->layout);
echo get_class($this->layout);
}
Knowing what $this->layout does not get changed into a View object means that the setupLayout() method is either not called or, more likely, not the result of View::make($this->layout) is not a proper view (perhaps it's silently failing for some reason).
The above steps hopefully give you a clue into whats happening there. Perhaps layouts.signup isn't a layout the app is finding?
What do your routes look like?
Change
class UsersController extends Controller
to
class UsersController extends BaseController
Hopefully the author of Confide fixes this :-)
I had been wondering why my error page caused certain pages of my site
not to render, but then I realized that it's because AppError extends
ErrorHandler instead of AppController. This caused some variables
that I set in AppController's beforeFilter method not to be sent to
the view. Since I can't access session variables from AppError, I
thought that I might be able to get away with using the classRegistry
to instantiate something that could and simply copying and pasting the
rest of my code from AppController's beforeFilter... but that isn't working, nor does it seem like a very elegant fix. Does anyone have any clues as to what
would be the best way to approach this? Thanks, David.
Your AppError class has a controller instance. You can call the beforeFilter manually:
<?php
class AppError extends ErrorHandler {
function error404() {
$this->controller->beforeFilter();
parent::error404();
}
}
?>
In CakePHP 2, you can do something like this to achieve the same effect. In app/Config/bootstrap.php, add this line:
Configure::write('Exception.renderer', 'AppExceptionRenderer');
Then create a file app/Lib/Error/AppExceptionRenderer.php with this code:
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
protected function _outputMessage($template) {
$this->controller->beforeFilter();
$this->controller->render($template);
$this->controller->afterFilter();
$this->controller->response->send();
}
}
Described more generally here: http://book.cakephp.org/2.0/en/development/exceptions.html#using-a-custom-renderer-with-exception-renderer-to-handle-application-exceptions
Edit: Updated link to point to correct location of the CakePHP 2.0 Book as of July 05, 2012.