How to get previous route name in Laravel 5.8 - laravel

I'm trying to get previous route name in Laravel 5.8. This answer (https://stackoverflow.com/a/40690569/16735772) works perfectly when I use Virtual Host but in direct url like (125.125.125.125/project_folder/public) does not because NotFoundHttpException is thrown.
With Virtual Host, $request->create(url()->previous()) creates a request with pathInfo and requestUri using relative url, for example, my_profile/2 but without Virtual Host, those attributes have different value, like project_folder/public/my_profile/2.
I don't need to redirect back, just know previous route name to check. For example:
if ($previous_route == 'show_name') {
//do something
}
Any help will be appreciated.

Try this code to get the previous route name:
Route::getRoutes()->match(
Request::create(URL::previous())
)->getName();
You can make a helper or blade directive to make it easier to access.

Related

Laravel URL Helper return IP address instead of Domain name

I have a problem when trying to create the Url using URL helper. I'm using Laravel 6.
$verify_url = url("/verify");
This is return the URL with the IP address instead of the domain name.
I don't know if it is a problem with the Apache server or the code.
Please help me. Thanks.
Adding this directive to Apache Virtual Host config seems to have fixed it: ProxyPreserveHost On
But a better way to use your urls is to name them in your routes/web.php. For ex:
Route::post('/verify', 'HomeController#verify')->name('verify');
and wherever you need to access to this url just use like this:
$verify_url = route('verify');

How the Url path from file path name to another name in laravel

Fins below the route code.
Route::get('clientlayout.main.index','InvoiceTicketController#show');
Route::get('clientlayout.main.mydomains','InvoiceTicketController#set');
When I run these routes I'm getting the url as
http://localhost:8000/clientlayout.main.index and
http://localhost:8000/clientlayout.main.mydomains.
I want my Url to be changed as follows: http://localhost:8000/index and http://localhost:8000/mydomains.
Suggest me a solution for changing the route to rectify this issue.
Route::get('/index','InvoiceTicketController#show');
Route::get('/mydomains','InvoiceTicketController#set');
for named routes you can use like this way
Route::get('/index','InvoiceTicketController#show')->name('clientlayout.main.index');
for more details follow
https://laravel.com/docs/5.6/routing#named-routes
You should try the route.
Route::get('/clientlayout/main/index','InvoiceTicketController#show');
Route::get('/clientlayout/main/mydomains','InvoiceTicketController#set');
Url is making
http://localhost:8000/clientlayout/main/index
http://localhost:8000/clientlayout/main/mydomains
Or You Should try
Route::get('/index','InvoiceTicketController#show');
Route::get('/mydomains','InvoiceTicketController#set');
Then Url is making
http://localhost:8000/index
http://localhost:8000/mydomains

Working with Laravel routes

I built my application using Angularjs on the frontend and Laravel 5 at the backend, however my main issue now is routing, when the page is loaded initially I set it to return my angular.php view I even added some code to catch all routes and return that view for me.
This does not work in all cases:
routes.php
Route::any('{url?}', function($url) {
return view('angular');
})->where(['url' => '[-a-z0-9/]+']);
Example of a URL that works with this is:
http://localhost:8000/tickets/events/catgeories/
Example of a URL that does not work with this is:
http://localhost:8000/tickets/events/Musical/Some-event-name
By "not working" I mean Laravel throws a NotFoundHttpException. What I am thinking right now is the above route can't go past three levels/parameters as in /level-1/level-2/level-3.
What am I doing wrong here?
Maybe because second URL has uppercase characters?

Laravel 5 Route Strange Behaviour

I have a Laravel site set up on a Homestead box, so I'm accessing it on sitename.app:8000. I have a route called "news" but when I try to go to sitename.app:8000/news I get oddly bounced out to sitename.app/news/.
If I change the routename to "news2" I can access the desired controller action as per normal at sitename.app:8000/news2. So somehow it's "news" itself that has become uncooperative - and I'm pretty sure that we aren't even getting as far as the NewsController, when I try to access that url.
Can anyone work out from these symptoms what might be going wrong? One "news"-related change I made at some point was to add $router->model('news', "App\News"); in the boot method of the RouteServiceProvider, but removing this doesn't seem to make the difference.
ETA: People keep asking for the routes.php file. I can literally remove everything from the file except
Route::get('news', function() {
return "hello world";
});
Route::get('news2', function() {
return "hello world";
});
and /news2 will work but /news will bounce me out. So I remain pretty convinced that the problem is somewhere deeper than routes.php...
I finally worked out what boneheaded action of mine had been causing this behaviour!
I had created a folder in /public named "news"... i.e. with the same name as an important route. Not sure exactly what havoc this was wreaking behind the scenes for Laravel every time a request for /news was being made, but one can assume it was nothing good.
Advice for anyone tearing their hair out over a route that "mysteriously doesn't work" - check your public folder for possible collisions!
This is a known issue Larvel missing port
The easiest way to solve this problem is to go to public/index.php and set the SERVER_PORT value.
$_SERVER['SERVER_PORT'] = 8000;
Don't forget to set the base url in the config if you are using links on website, the url generator uses the base-url defined in the config.
Last option is to change the vm portfoward in VagrantFile to point to port 80 and use port 80 for your app.

Laravel 4 - changing resource root routing path

In a Laravel 4 installation, Using Jeffrey Way's Laravel 4 Generators, I set up a 'tweet' resource, using the scaffolding command from his example:
php artisan generate:scaffold tweet --fields="author:string, body:text"
This generated the model, view, controller, migration and routing information for the tweet type. After migrating the database, visiting http://localhost:8000/tweets works fine, and shows the expected content.
The contents of the routes.php file at this point is:
Route::resource('tweets', 'TweetsController');
Now I would like to move the url for tweets up one level into admin/tweets, so the above url should become: http://localhost:8000/admin/tweets. Please note that I am not treating 'Admin' as a resource, but instead just want to add it for hypothetical organizational purposes.
Changing the routes.php file to:
Route::resource('admin/tweets', 'TweetsController');
Does not work, and displays the following error:
Unable to generate a URL for the named route "tweets.create" as such route does not exist.
Similarly when using the following:
Route::group(array('prefix' => 'admin'), function() {
Route::resource('tweets', 'TweetsController');
});
As was suggested in this stackoverflow question.
Using php artisan routes reveals that the named routes also now have admin prefixed to them, turning tweets.create into admin.tweets.create.
Why is the error saying that it cannot find tweets.create? shouldn't that automatically be resolved (judging by the routes table), to use admin.tweets.create?
How can I change my routing so that this error no longer occurs?
I just tested with new resource controller and it works fine for me.
The problem is not with the Route, its with the named routes used in your application.
check your view files there are link to route like link_to_route('tweets.create', 'Add new tweet'), this is creating the error because when you add admin as prefix tweets.create doesn't exists so change it to admin.tweets.create every where, in your controller also where ever named route is used.

Resources