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;
Related
I cloned a copy of Codeigniter from github today using this link: https://github.com/bcit-ci/CodeIgniter.git. I am currently running MAMP setup so that http://localhost:8888 points to my htdocs folder. My root folder is called 'time'. When I go to http://localhost:8888/time/index.php, I do see the Codeigniter welcome page. Also, I can go tohttp://localhost:8888/time/ and see the same welcome page, even though I don't have an .htaccess file in the root directory.
Here is the problem. I added the following function to the Welcome.php controller class:
public function test()
{
echo 'Test';
}
This should display a page which shows 'test' when I visit http://localhost:8888/time/index.php/test. However, I get a 404 page not found error. Does anyone have any suggestions for understanding and fixing this problem?
Because localhost/index.php/test doesn't refer to the method test in the welcome controller. You would have to go to localhost/index.php/welcome/test or use routes.
They way you are doing it implies there is a controller named Test.php and it is trying to go to the index() function of that controller.
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');
}
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
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.
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.