Laravel Routing Does not Work - laravel

I am new in LARAVEL framework and I want to run a controller with just function of view a page
class TestController extends BaseController {
public function index()
{
return View::make('hai');
}
}
I set the routing in routes.php file as given below
Route::get('test','TestController#index');
I tried to run in mozilla with
localhost/laravel/public/test
but it shows
Not Found
The requested URL /laravel/public/test was not found on this server.
is there any problem in my .htaccess page ?
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Any body please help me.

I'm assuming that you are on Ubuntu machine. To make it work, at first, enable the rewrite module by executing following command in the terminal
sudo a2enmod rewrite
Second, find "apache2.conf" file in your system, mine is located in
/etc/apache2/apache2.conf
In this file, find the following snippets of code:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change "AllowOverride None" to "AllowOverride All". Save the file and restart the apache server by executing following command
sudo service apache2 restart
This should solve the issue. Cheers

Precondition: you will know that you have htaccess working, if when you first installed laravel and went to localhost/laravel/public/, you'd see the laravel logo.
The route is:
Route::get('/', function()
{
return View::make('hello');
});
Regarding Your route: you don't need to explicitly state the function (#index) in the route. Laravel has a default path it follows based on the HTTP request type. Check here (under the heading Actions Handled By Resource Controller) for more details on that.

Simply I refer you not modifying .htaccess file. From http://tisuchi.com/easy-way-handle-routing-laravel/:
Static Page Handling
Handling static page in laravel is one of the easiest way to handle
route. Open app/routes.php file and deleted everything from that.
Assume that, we are try to route into our following pages- home
(example.com), about (example.com/about), contact
(example.com/contact). To do so, just write following code in your
routes.php file-
/**
* Static page code
*/
Route::get('/', function(){
return View::make('home');
});
# For example.com/about
Route::get('about', function(){
return View::make('about');
});
/**
* Naming Route for static Page
*/
# For example.com/contact
Route::get('contatct', array('as' => 'contact', function(){
return View::make('contact');
}));
If you are really new in Laravel, highly recommend to look into following 2 tutorials-
http://freshwebdev.com/best-laravel-tutorial-beginners.html
http://tisuchi.com/easy-way-handle-routing-laravel/
Ofcourse, don't forget to take a look into official page of laravel.com.
Hope, it will work for you.
Have fun...

Related

How to set a masked redirect in Laravel?

How to set a redirect of the www.domain.com (only route('index')) to web.domain.com on www, which has a Laravel app and web holds Wordpress? I have an access to domain configurations.
You could use a (masked) URL frame, e.g, if I understand your hierarchy correctly:
Route::get('/index',function(){
return view('blog');
});
block.blade.php:
<html>
<body>
<iframe src="http://web.domain.com"></iframe>
</body>
</html>
or try a rewrite without redirect on the webserver level. If you are using Apache:
A rewrite without redirect requires enabled mod_proxy, as well as mod_rewrite and .htaccess through Apache's httpd.conf. (Unless it's the same VirtualHost, it cannot be done without mod_proxy, see Absolute URL in the Substitution section in the refs. It "always" forces an external redirect.)
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#if not already web.mydomain.com
RewriteCond %{HTTP_HOST} !^web\.mydomain\.com$ [NC]
#rewrite request is for www/* to web.mydomain.com
RewriteRule ^index/?$ http://web.mydomain.com/$1 [L,P,NC]

laravel routing returning 404 without using index.php

I am having a small issue creating a dynamic sitemap.xml for the site.
My routing file:
Route::get('sitemap.xml', [
'uses' => 'PageController#sitemapXml',
'as' => 'user.page.sitemapxml'
]);
My controller:
public function sitemapXml(){
....
$content .= '</urlset>';
return response($content, 200)->header('Content-Type', 'text/xml');
}
My issue:
To access get the correct response i have to use the following route:
www.mysite.com/index.php/sitemap.xml
However, if i do www.mysite.com/sitemap.xml, i get a 404.
If i manually add the sitemap.xml file in my public folder, i can access the file i just added, but not the dynamic one created in my controller.
My research:
Its probably a problem with the server and not laravel itself. Apparently, .xml extension are not processed through the normal routing.
It suggests adding:
location = /sitemap.xml {
try_files $uri $uri/ /index.php?$query_string;
access_log off;
log_not_found off;
}
that did not work.
I also tried the changing my .htaccess to this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
as specified in the docs
My question:
Is their anything I can do laravel-wise to handle .xml requests?
Also, if you could guide me towards anything that might help me understand this process better, its very welcomed.
Thank you in advance for taking the time to help
UPDATE:
if instead of using my localhost, i use "php artisan serve", things work correctly without any issue.
I found the answer on this link:
https://laravel.io/forum/09-15-2015-removing-indexphp-from-url-laravel-5116
I changed my .conf file and voila.
Hope this helps you
Just to clarify: Nothing wrong with Laravel. More server specific.

Laravel4 NotFoundHttpException in Wamp Server

I am reading Laravel4 quick start tutorial
Laravel4 quick start
I have installed laravel4 on Wamp server in Windows . I was able to access home page which says "you have arrived".
Later in ther router.php I have added only one router as per quick start guide, but for some I am getting this error.
Error:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Router.php
Route::get('/', function()
{
return View::make('hello');
});
Route::get('/users', function() //this one i hav added
{
die;
return 'Users!';
});
config.php
'debug' => true,
'url' => 'http://localhost:99',
'timezone' => 'UTC',
'locale' => 'en',
Do not prefix the route with a slash. Use users rather than /users.
I had exactly the same problem today and it took a while to resolve it, but here's how I did it. In this example I created a WAMP Apache Alias 'lava', so http://localhost/lava/ took me to the "You have arrived" page successfully. However even trying a really basic route of:
Route::get('test', function()
{
return "hello?!";
});
Would give me the NotFoundHttpException errors the same as you. To get routing working I did this:
Ensure mod_rewrite is enabled (WAMP does it by default, but double-check)
Ensure open_ssl is enabled (easy to do via the WAMP tray icon)
Edit app/config/app.php and set the url to match your alias, so in my case:
'url' => 'http://localhost/lava/',
Edit public\.htaccess - you can use the one the QuickStart recommends, but you need to change it in 2 places:
```
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /lava/
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /lava/$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
```
In the above I've added the RewriteBase line which matches the alias, and also added the alias in the RewriteRule regexp.
The above for me works fine (Windows 7, WAMP) although the .htaccess file is no use when deployed to a live server that doesn't use the same alias.
Trying going to index.php/users. If this loads then you need to setup url rewriting in the htaccess file.

Code Igniter doesn't recognize my function in controller under a folder

I have a folder under my main controller-folder called admin, in that controller i have a file name admin.php which has a function xyz.
I want to access that function using this url
http://localhost/webroot/admin/xyz
However when I try to access it, it is giving me this error.
404 Page Not Found
The page you requested was not found.
this is code of my routes.php file
$default_controller = "welcome";
$controller_exceptions = array('welcome','forum');
$route['default_controller'] = $default_controller;
$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1';
$route['404_override'] = '';
$route['admin'] = "admin/admin";
This is my .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Also mod-rewrite is enabled :)
Please let me know why is it not working and how can i make it work. Any kind of help is really appreciated. Thanks
Ensure that your routes are stored in application/config/routes.php (not router.php as specified in your question). Try adding a route that uses a wildcard, like this:
$route['admin'] = "admin/admin"; //Routes to 'index()' function
$route['admin/(:any)'] = "admin/admin/$1"; //Handles all other cases
:any will match a segment containing any character(s), after the admin segment, and will pass it/remmap it to the the admin controller.
The user guide contains more information on controllers in sub-folders and routing.

jQuery - Address plugin problem

I've come across a problem which i cant seem to figure out, i use the jQuery address plugin to store history and enable deep linking, and a typical url after a click would look like this:
http://mysite.com/#!/page
Problem here is i need rid of the last / so i need it to look like this:
http://mysite.com/#!page
I'm using plugin version 1.2 - the latest is 1.4. When i use 1.4 my hashbang #! disappears..
Anyone know why? even so, the updated version produces the same problem.
Reasons to fix this are i use 301 redirects to 'Pretty URL's' if an ?_escaped_fragment_= is requested. So this:
http://mysite.com/data/#!page1
would become:
http://mysite.com/data/page1
currently it does this: mysite.com/data//page1
here is the .htaccess rewrite:
<IfModule mod_rewrite.c>
RewriteEngine on
# Rewrite current-style URLs of the form 'index.php?url=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>
and here is some relevant PHP i use on page load:
if ($fragment = $_GET['_escaped_fragment_']) {
// OPTION 1: if Google is reqesting an '_escaped_fragment_=' page, then redirect to a clean URL
header("Location: $base/$fragment", 1, 301);
exit;
}
Any help on how to make this situation better is appreciated.. I don't want 'use the HTML5 History API' as ive explored this option already.
This line of code can help!
$.address.strict(false);

Resources