Missing some more adjustments to make routing work in Laravel - php

Following this http://laravel.com/docs/quick#routing
I add this:
Route::get('users', function()
{
return 'Users!';
});
to the bottom of /app/routes.php and GET it from my browser http://localhost/users. But the returned result is users Not Found.
I've also tried a route of /users too, and this does the same.

Oh no, the route should be:
http://localhost/index.php/users
That should work.

Related

How to call route which is not defined in laravel

I am newbie to laravel and i am working on a project and i have a following situation
lets assume my base url is https://example.com
Now i want to pass a slug(argument) after a base url which means https://example.com/xyz something like that, and i need to do this on multiple times in my project
This is what i'd tried but it is not working it says that route is not defined.
Route::get('{slug?}', [App\Http\Controllers\UiviewsController::class, 'method1'])->name('method1');
Route::get('/method2/{slug?}', function($slug){
return redirect()->route('method1', ['slug'=>$slug]);
});
And also how can i achieve that on which argument which particular method should be called? for example if i have several other routes similar to above one.
how can i achieve this?
Thank you in advance for your help. :)
You should use the fallback system.
Route::fallback(function () {
//
});
Laravel Route fallback official docs
also, beware:
The fallback route should always be the last route registered by your
application.
Other Option:
Also, you can define a parameter as below example
Route::any('{any}', function(){
//...
})->where('any', '.*');
Please Try php artisan route:cache in your terminal then check it again.

Laravel routing strange behavior

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');
});

Can't route properly in Laravel

I am very new to laravel and I simply don't get how to route properly, am completely lost. The problem is I can only route a view to localhost/public. For example I can't route a view to "localhost/public/something". Am not even sure my urls are correct. If i leave "something.php" empty nothing will show, but if i add some html it shows only the html no views. Am sure am doing it all wrong. So how does one route a view to a page other than "public/".
The routing for index is
Route::get('/', function(){
return View::make('welcome');
});
works okay.
Now how can I achieve the same if do
Route::get('something', function(){
return View::make('welcome');
});
or
Route::get('/something', function(){
return View::make('welcome');
});
So i finally got it to work after so many hours. I deleted my laravel installation and did a new one and the problem is gone. Thanks.
There's nothing wrong with your code but might be for .htaccess
Please try accessing the route via following URL
http://localhost/public/index.php?/something
if it's working, you need to use the alternative .htaccess file provided on Laravel website or see if mod_rewrite is loaded on your Apache.

Route::get('(:any)',function() not working properly in laravel 4

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

Accessing route::resource using https in Laravel 4

To force HTTPS on a named route, the Laravel docs say do the following:
Route::get('foo', array('https', function()
{
return 'Must be over HTTPS';
}));
Now, on my first Laravel app, I have been using resource controllers. I don't think I will be using them for my second app, going on what I have since read, but for now they sit happily in my router.php file.
I wanted to force the back office part of my app to use HTTPS. So, my opening gambit was as follows:
Route::resource('backoffice', array('https','BackofficeController'));
Laravel didn't like the array.
So, instead I thought I would try putting at the next parameter:
Route::resource('backoffice', 'BackofficeController', 'https'));
But the next parameter needs to be an array. I could find no documentation on this, but I converted it to array. It still didn't work.
Route::resource('backoffice', 'BackofficeController', array('https')));
I even tried:
Route::resource('backoffice', 'BackofficeController', array('https'=>true)));
However, that failed too. So, how do I force a resource to use https?
Route::filter('forceHttps', function($req){
if (! Request::secure()) {
return Redirect::secure(Request::getRequestUri());
}
});
Route::group(['before' => 'forceHttps'], function(){
Route::resource('backoffice', 'BackofficeController');
});
Assuming you have a filter function like the one Andreyco suggested, which seems fine, you could do something similar to this:
//Andreyco's filter
Route::filter('forceHttps', function($req){
if (! Request::secure()) {
return Redirect::secure(Request::getRequestUri());
}
});
//backoffice group routing
Route::group(array('prefix' => 'backoffice', 'before' => 'forceHttps'), function()
{
Route::any('/', 'App\Controllers\BOindexController#index');
Route::resource('otherBackOfficeURI', 'App\Controllers\OtherBOController');
//other routes & controllers here...
});
This way, everything starting with site.tld/backoffice will go through the https filter (and most probably through a isAdmin filter) and then check inner function route rules. I think this will be more convenient.

Categories