I have two routes.
The second stopped working so I added the first one (which works perfectly).
Routes file
Route::get('/cms/index', function () {
return view('cms-templates/index');
});
Route::get('/cms', function () {
return view('cms-templates/index');
// tried return redirect('/cms/index')
// tried return hello world
});
The problem is it doesn't matter what I try to return in the second route, it always throws a page with error 404 Not Found ------ nginx/ver instead of the NotFoundHttpException or InvalidArgumentException that usually come when not finding route or file (which still happens with which other route)
Try this:
Route::get('/cms/index', function () {
return view('cms-templates.index'); //cms-templates should be a folder with a blade called index
});
Anyway you're trying to return the same view for two differents Routes?.
Also you can call to a controller and her function using this:
Route::get('/cms/index','namecontroller#function');
Feel free to ask if have any doubt.
Related
If we just create a 404.blade.php page in resources/views/error it will work fine, but Auth() won't work on 404 page, to solve that if we follow the solution available on stackoverflow the Laravel auth errors will stop working. I use the following solution to do the work.
Create custom view
resources/views/errors/404.blade.php
in route.php
Route::any('{catchall}', 'PageController#notfound')->where('catchall', '.*');
create PageController and add this function
public function notfound()
{
return view('errors.404');
}
For Laravel 5.6 and later, you can use fallback in your routes\web.php:
Route::fallback('MyController#show404');
It works as an "catch all"-route.
See docs here.
Write below code in your Exceptions/Handler.php
if($this->isHttpException($exception)){
if(view()->exists('errors.'.$exception->getStatusCode())){
$code = array('status'=>$exception->getStatusCode());
return response()->view('errors.404',compact('code'));
}
}
Now create a new file in your view i.e errors/404;
Now in code array you can pass dynamic values
Not sure if this will help. Laravel has a PHP artisan command to publish error pages. Laravel Custom HTTP Error Pages
After you run this artisan command use Route:fallback() method as #KFoobar suggested. If you use a closure function, no need to use a controller. Make sure to add the below route at the ends of your routes file.
//Fallback/Catchall Route
Route::fallback(function () {
return view('errors.layout');
});
Shameless plug for my BLOG
I have a single domain/subdomain project. In order to see the event by slug, I made this route:
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
});
After my page didn't load correctly, I did a dump in the controller:
public function getView($slug)
{
return $slug;
}
To get to the route I am using this URL: https://example.com/events/slug-example.
The problem is that the route is being hit as I see the response when I change it, but I am not getting the slug, instead I am getting Region object back.
If I do this:
public function getView($region, $slug)
{
return $slug;
}
Then I get the slug back. But I have no idea how is this possible, and how could I do it (I came as another dev on the existing project).
I tried commenting out all the middleware and it is still the same. How can I even make something fill the method if I didn't explicitly say it?
EDIT
I noticed there is binding going on in routes file:
Route::bind('region', function ($value) {
...
});
Now if I dd($value) I get the variable back. How is this value filled? From where could it be forwarded?
Looking quickly it should work, but maybe you was verifying other url.
Make sure you put:
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
routes at the end of routes you showed.
EDIT
If you think that's not the case and you don't have your routes cached you should run:
php artisan route:list
to verify your routes.
EDIT2
After explaining by OPs in comment, domain used for accessing site is:
{region}.example.com
So having $region in controller as 1st parameter is correct behaviour because of route model binding and other route parameters will be 2nd, 3rd and so on.
Instead of
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
});
try
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
});
The program is a URL shortener that I am just trying out from some tutorial ( for Laravel 3 but i am using laravel 4) and it is supposed to give me the "some URL" as output when I click on the shortened URL.
<?php
Route::get('/', function()
{
return View::make ('home.index');
});
Route::post('/',function(){
$url = Input::get('url');
// If the url is already in the table, return it
$record=Url::whereurl($url)->first();
if ($record) {
//then return it
return View::make('home.result')
->with('shortened', $record->shortened);
}
});
Route::any('{shortened}',function($shortened)
{ echo "Everything is ok with: ".$shortened; })
->where('shortened', '.*');
Rather it is going to a error page saying
"Not Found The requested URL /asdf was not found on this server."
I think it is a very simple error on my part. I am not sure whether they have changed the syntax or the keyword. I am not able to find any solution from the laravel docs as well.
I use the following for my CMS to catch all requests with a Regex filter:
Route::get('{uri}', 'PublicController#init')
->where('uri', '.*');
In your case it would probably look like this:
Route::get('{uri}', function($uri) {
return 'Hello, world!';
})->where('uri', '.*');
You can use the first one to load a method inside of a controller. The controller being PublicController and the method init. There you can handle your errors initiate any other processes based on the request.
This works really well, you can add reserved routes before ths route so it becomes the last route catching all requests that are not catched by previous routes.
In Laravel 4, you do:
Route::get('{shortened}', function($shortened)
instead of
Route::get('(:any)',function($shortened)
You can read more about route parameters in Laravel 4, here: http://laravel.com/docs/routing#route-parameters
Lauch Laravel
go to you'r application root,run the command PHP artisan serve
Put the routing
-Comment evrything in routing.php, add these routes:
Route::get('/', function()
{
echo 'hello';
});
Route::any('{shortened}',function($shortened){
echo "Evrything is ok with: ".$shortened;
})->where('shortened', '.*');
Launch you application
-go to http://localhost:8000/asdf
I use laravel 4 and am not able to show a component :
this is a line from my routes
Route::resource('/', 'PostsController');
and this is my show function from PostsController.php
public function show($id) {
return "HI";
}
And This is the line that links to the function from my view
<h1>{{$post['title']}}</h1>
And It properly links to localhost:8000/show/1
But I'm amazingly getting a Not found HTTP exception from laravel.
How do I get this to work?
So the answer is that #NihalSahu is wrong (me) and that resourceful routing doesn't work like that it would be infinitely better so as to set the router to host/{id}
I have a problem with my app on the server.
When I access it through various links, it works. But, when I put the URL to the same site in my browser, I get a NotFoundHttpException. What I have detected in the message of the exception are the following problems:
REDIRECT_URL /app/public//login
REQUEST_URI /app/public//login
I do not understand why it adds two slashes (//) after public instead of one (/).
The code for my file route.php is:
Route::get('login', 'UserController#get_index');
And, the code for my Controller is:
public function get_index()
{
return View::make('admin.login');
}
I think changing the naming convention to the one laravel uses will fix this problem.
Do this in your routes:
Route::get('login', 'UserController#getIndex');
and this in the controller:
public function getIndex()
{
return View::make('admin.login');
}