How to: make my routes start working with Laravel 4? - php

I am new in Laravel (v4.0.5) and according the website http://laravel.com/docs/routing and other book I found, make a route its as easy as this
Route::any('foo', function()
{
return 'Hello World';
});
so, I try at my host
http//myhost/public/foo
(and I have to use /public due if i only enter to my host directly where is the folder, I see the structure of the framework... anybody know why is that?)
But I get not found
I've tried
Route::any('user','UserController#index');
I have created my controller like this
<?php
class UsersController extends BaseController {
public function showWelcome()
{
return View::make('users');
}
public function getIndex()
{
return View::make('users');
}
}
And tried with /users but nothing...
Any idea what I'm doing wrong?

Try going here:
http://myhost/public/index.php/foo
You will need to setup URL rewrites if you want it to work with
http://myhost/public/foo
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

You should point the web root of your server to the public folder. If you're using Apache, you can change this on the httpd.conf file, looking for DocumentRoot setting. Example:
DocumentRoot "C:\Users\Raphael\Documents\GitHub\RaphStore\public"
This is important because it makes sure no one has access to your project files, as it should be.

Related

Routing with index file laravel 5.4

I'm trying to setup my laravel project but I cant seem to get routing to work. This is my routes/web.php:
Route::post('test','UserController#test');
Route::resource('/','UserController');
Now my / works. The functions in it work, but Laravel says my test does not exist. I get a 404 error.
In my / index I have a form like this:
<form method="POST" action="test">
It goes from laravel/public/ to laravel/public/test. But apparently laravel/public/test gives back a 404 error. I tried to fix it with: Route::post('/test','UserController'); but it gives the same error. The only 2 differencex with the documentation that I see is that I'm working from / which shouldnt make a difference(?) and that I'm not working directly from localhost/ but with some maps where I stored my project. Which shouldnt make a difference either. What am I doing wrong here?
EDIT:
My controller:
class UserController extends Controller{
public function index()
{
return view('testindex');
}
public function test(){
return 'test';
}
}
You should then fix it by one of the following:
public function postTest(){
return 'test';
}
or in routes/web.php
Route::post('test','UserController#test');
You're using wrong web server settings. You need to point web server to the public directory inside Laravel root and use correct settings.
For Apache:
DocumentRoot "/path_to_laravel_project/public"
<Directory "/path_to_laravel_project/public">
For nginx:
root /path_to_laravel_project/public;

Laravel unable to load view

Just started learning laravel I tried to create a route for my view but when I load on the web browser it says Sorry, the page you are looking for could not be found. Can any one help me out in codeigniter it was so simple just create a controller and view and we can see on the web broswer I found laravel to be difficult then codigniter is that true?? can anyone define me how is the mvc structure for laravel5 as i found tutorials but they are of old laravel and files and structure are almost change so I got confused any suggestions please
routes.php
Route::get('main', 'Main#index');
Main.php
namespace App\Http\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class main extends Contoller {
public function _construct() {
$this->middleware('guest');
}
public function index() {
return "Hello World from controller";
}
}
if you are running laravel project locally, it can run through its own server. you dont need apache server form wamp or xampp,,but you will need their mysql database. So start only that if you require database.
Now go to command prompt, navigate to the directory where your project is stored eg cd c:/wamp/www/yourprojet and then type following command
php artisan serve
it will start on port 8000 by default. and you can now access your project at 'http://localhost:8000/'
and you can access your view at 'http://localhost:8000/main'
Also you can find laravel tutorials and other at laracast
Try to change the class name to Main (now is main, in lowecase)
I learnt Laravel by their awesome tutorial: https://laravel.com/docs/4.2/quick
make a view in resources->views, something like my-view.blade.php
Then return view('my-view');
That my-view.blade.php can have whatever HTML you want in it
Go to your resources/views folder create file with filename.blade.php.
Now in your routes.php:
Route::get('main', 'Main#index');
And in your controller add this function:
public function index() {
return view('filename');
}

how to invoke a specific controller action in laravel

I am very new to Laravel. I created new Controller as
class ContactController extends Controller {
public function index(){
die("X");
}
}
And in the routes.php I wrote
Route::get('contact', 'ContactController#index');
I think hitting the following url the "X" must be printed. But it says "Not Found" error.
http://localhost/lapp/public/contact/index
What thing I am missing? Please guide me.
If the apache module_rewrite is not activated. Activate it. It will surely work.
Your guess is right. But the problem is with you URL.
it should be http://localhost/lapp/public/contact
Alternatively you can use php artisan serve command to start a server which would start the server on something like http://localhost:8080 Then you can access the URL like: http://localhost:8080/contact

404 error laravel 4 routing

I am having troubles getting laravel to find a page when I route to it. My route is setup and being recognized as when I create link using URL::route('account-create') laravel will successfully parse that into '/account/create' as to where I want the link to go to. But upon clicking it I get a 404 error.
My route
Route::get('/account/create', array(
'as' => 'account-create',
'uses' => 'AccountController#getCreate'
));
My controller
class AccountController extends BaseController {
// view the create user form
public function getCreate()
{
//return View::make('account.create');
}
}
The create.blade.php is created and put inside an account folder under app/views. commenting or uncommenting makes no difference. The 404 remains.
I have also tried:
Route::get('/account/create', function()
{
return 'Hello World';
});
in my routes.php still the 404 remains
Is there some configuration I am missing?
After looking around some more I discovered I needed to enable rewrite_module on apache. Makes perfect sense really, how can laravel make clean urls if it cant rewrite the url. I knew it was going to be something simple to fix.
Thanks a lot for your replys
Does navigating to index.php/account/create work? If so, it is probably an error in your root-level .htaccess file, or in your VirtualHost settings in httpd.conf on your server. Post the contents of .htaccess from app/public.

"Whoops, looks like something went wrong." Laravel 4.1

Okay, so I've just gotten around to checking out Laravel 4.1, I've hit a problem and I don't understand why? The problem is that I get this message on the screen "Whoops, looks like something went wrong." but I haven't really done anything. I've created a view, a controller and added a route and this is what is in these files
VIEW
<h1>Author's home page</h1>
CONTROLLER
class AuthorsController extends BaseController {
public $restful = true;
public function getIndex () {
return View::make('authors.index'); //authors.index because it's in the authors folder within the views folder
}
}
and ROUTE
Route::get('authors', 'AuthorsController#getIndex');
So logic dictates that when I go to the authors URL it should load the getIndex function within the AurhorsController page and show the index.blade.php file which is in views > authors.
If so then I have no idea why this is not working! any help would be appreciated. Thanks in advance.
EDIT 1
This is the actual error
throw new NotFoundHttpException();
EDIT 2
You should remove public $restful = true; but it's not a problem for NotFoundHttpException
// Path: app/controllers/AuthorsController.php
class AuthorsController extends BaseController {
// public $restful = true;
public function getIndex () {
return View::make('authors.index');
}
}
According to your route given below:
Route::get('authors', 'AuthorsController#getIndex');
It should work if you make a GET request, make sure you made the request from browser's address bar and url was like yourdomain.dev/authors or http://localhost/yourapp/authors. Also you may run following command from command prompt/terminal (within your project directory):
composer dump-autoload
Looks like Laravel's trying to access the public folder and fails, because there's no file named authors in that folder, and also no route named public/authors. You'll want to go to http://localhost:8081/branch/authors instead, assuming that your installation resides in http://localhost:8081/branch.

Categories