Laravel 5 new routes don't work - php

I'm starting with laravel 5 framework and I have a problem with the routes.
In the last few days, the routes worked correctly, but today I added a new route and it doesn't work anymore.
I have these routes
Route::get('url/create', 'UrlController#create');
Route::get('url/bulk', 'UrlController#bulk_view');
Route::post('url/bulk', ['as' =>'url/bulk', 'uses' => 'UrlController#bulk']);
Route::get('url/bulk_metrics', 'UrlController#bulk_metrics_view');
Route::post('url/bulk_metrics', ['as' =>'url/bulk_metrics', 'uses' => 'UrlController#bulk_metrics']);
Route::post('url/create', ['as' =>'url/create', 'uses' => 'Urlcontroller#store']);
Route::post('url/update/{id}', ['as' =>'url/update', 'uses' => 'Urlcontroller#update']);
Route::get('urls', ['as' =>'url/list', 'uses' => 'Urlcontroller#index']);
Route::get('url/{id}', ['as' =>'url/show', 'uses' => 'Urlcontroller#show']);
Route::post('url/delete/{id}', ['as' =>'url/delete', 'uses' => 'Urlcontroller#destroy']);
All work correctly, but I added this new route
Route::post('urls/filter', ['as' =>'url/filter', 'uses' => 'Urlcontroller#filter']);
and I call it like this
{!! Form::open(array('route' => 'urls/filter', 'method' => 'POST')) !!}
I tried php artisan route:clear, php artisan route:cache and php artisan route:list, and the new route appear in the list:
POST | urls/filter | url/filter | App\Http\Controllers\Urlcontroller#filter | web,auth |
The other routes work correctly and I think that it is a cache problem, because if I change the url/create to url/create2, and I change it in the template to url/create2 it doesn't work.
Thanks in advance to all

You should use it as url/filter
{!! Form::open(array('route' => 'url/filter', 'method' => 'POST')) !!}
because you're naming it like this:
'as' =>'url/filter'
Or remove 'as' =>'url/filter' part from route. In this case name of your route will be urls/filter and not url/filter.

Related

Laravel 8 how to name a group

This question has been asked but it's outdated in most answers I saw.
This is what I'm trying:
Route::prefix('cart')->group(function() {
Route::get('/mycart','App\Http\Controllers\frontend\CartController#mycart')->name('mycart');
Route::get('/checkout','App\Http\Controllers\frontend\CartController#checkout')->name('checkout');
})->name('cart');
I'd like to name the group and then in my blade be able to do:
#if (Route::currentRouteName() != 'individual_product')
The above gives me this error: Call to a member function name() on null
Any idea what i'm doing wrong?
One can configure everything with arrays:
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => 'cart', 'name' => 'cart.'], function() {
Route::get('/index', ['as' => 'index', 'uses' => 'CartController#index']);
Route::get('/checkout', ['as' => 'checkout', 'uses' => 'CartController#checkout']);
});
And 'as' => 'cart.index' might still be more readable than a named group (it may depend).
In Blade one can also use Route:
#if (Route::has('cart')) #endif
#if (Route::has('cart.index')) #endif
Listing all defined routes is being supported:
php artisan route:list
You can use name prefixes like below:
Route::name('cart.')->prefix('cart')->group(function() {
Route::get('/mycart','App\Http\Controllers\frontend\CartController#mycart')->name('mycart');
Route::get('/checkout','App\Http\Controllers\frontend\CartController#checkout')->name('checkout');
});
for more : https://laravel.com/docs/8.x/routing#route-group-name-prefixes
in blade you can check
#if(request()->routeIs('cart.*')) // or ->routeIs('cart.mycart')
#endif
https://laravel.com/docs/8.x/requests#retrieving-the-request-path

Laravel route optional parameter

i have one issue with the Laravel routes.
I have one function index($sport = '', $date = ''); this function shows me the news for a specific sport and date. But sometimes these parameters are not entered and I want to display all news. This works so fine so good, the problem is with the route.
This is the route code I have used:
Route::get('/news/{sport?}/{date?}.html', ['as' => 'news.index', 'uses' => 'NewsController#index']);
The problem comes when there is no sport and date entered, than the URL is domain.com/news.html, but is not caught with that code. How can i achieve that ?
If you think about news and sport as resources you could have something like that:
// index case news
Route::get('/news.html', ['as' => 'news.index', 'uses' => 'NewsController#index']);
// show case news
Route::get('/news/{date}.html', ['as' => 'news.show', 'uses' => 'NewsController#show']);
// index case sport
Route::get('/news/sport.html', ['as' => 'sport.index', 'uses' => 'SportController#index']);
// show case sport
Route::get('/news/sport/{date}.html', ['as' => 'sport.show', 'uses' => 'SportController#show']);
A different approach would be:
// index case news
Route::get('/news.html', ['as' => 'news.index', 'uses' => 'NewsController#index']);
// sport case
Route::get('/news/{sport}.html' , ['as' => 'sport.index', 'uses' => 'SportController#index'])
->where(['sport' => '[0-9]+']);
// date case
Route::get('/news/{date}.html', ['as' => 'news.show', 'uses' => 'NewsController#show'])
->where(['date' => '[0-9]{4}-[0-9]{1,}-[0-9]{1,}']);
// sport and date case
Route::get('/news/{sport}/{date}.html' , ['as' => 'sport.index', 'uses' => 'SportController#show'])
->where(['sport' => '[0-9]+', 'date' => '[0-9]{4}-[0-9]{1,}-[0-9]{1,}']);
theres so many ways todo that, but for the simplest way, just add another route for displaying all list, Route::get('/news/','NewsController#index');
If you want to keep it simple , you should try this approach
domain.com/news.html?date=2016-11-23&sport=1
if date and sport not found in url you can show all result
and it is easy readable syntax if some one want to bookmark this url.

Named Routes Conflict Laravel 5.2

I am creating a Multilingual Laravel 5.2 application and I am trying to figure out how to change language when I already have the content.
routes.php
...
Route::get('home', [
'as' => 'index',
'uses' => 'SiteController#home'
]);
Route::get([
'as' => 'index',
'uses' => 'SiteController#inicio'
]);
...
I have SiteController#home and SiteController#inicio. So I change session('language') in SiteController#change_language like:
...
public function change_language ($lang){
session(['language' => $lang]);
return redirect()->action(SAME NAMED ROUTE, DIFFERENT LANGUAGE);
}
...
So, When I click on a button with
English
from /inicio (SiteController#inicio) I should be redirected to the same named route (SiteController#home) so I can check the language and display appropriate content.
Any ideas of how to get the named route or something helpful?
Thank you :)

Laravel-5 Redirect Loop

My routes.php like this:
Route::get('deneme/{test}', 'TestController#index');
Route::get('send', 'MailController#index');
Route::get('elfinder/tinymce4', 'Barryvdh\Elfinder\ElfinderController#showTinyMCE4');
Route::group(['middleware' => ['auth', 'perm'], 'prefix' => 'admin'], function(){
Route::get('/', ['as' => 'admin', 'uses' => 'Admin\DashboardController#index']);
Route::resource('kategori', 'Admin\KategoriController');
Route::resource('icerik', 'Admin\ContentController');
// Property Routes
Route::resource('property', 'Admin\PropertyController');
Route::post("property/lang", ['uses' => 'Admin\PropertyController#langAdd', 'as' => 'admin.property.lang_add']);
Route::get("property/lang/{id}", ['uses' => 'Admin\PropertyController#langDelete', 'as' => 'admin.property.lang_delete']);
Route::get('users/add', 'Admin\UsersController#add');
Route::get('content/add', 'Admin\ContentController#add');
});
Route::get('admin/login',['as' => 'admin.get_login', function(){
return view("admin.login");
}]);
Route::post('admin/login', ['as' => 'admin.do_login', 'uses' => 'Auth\AuthController#postLogin']);
Route::get('admin/logout', ['as' => 'admin.logout', 'uses' => 'Auth\AuthController#getLogout']);
When I send request to site.dev/admin the browser say:
This webpage has a redirect loop
ERR_TOO_MANY_REDIRECTS
I have delete rotation and leave blank only PHP tag available but this url -site.dev/admin- return same error. I tested 2 browsers, Chrome and Firefox. I think this error stems from browser-cache and installed Opera browser -not installed before- but still get same error.
I solved this problem. Under the public folder exist a folder with name is admin. So i changed my rotation like this:
Route::group(['middleware' => ['auth', 'perm'], 'prefix' => 'adminpanel'], function(){
Route::get('/', ['as' => 'admin', 'uses' => 'Admin\DashboardController#index']);
Route::resource('kategori', 'Admin\KategoriController');
Route::resource('icerik', 'Admin\ContentController');
// Property Routes
Route::resource('property', 'Admin\PropertyController');
Route::post("property/lang", ['uses' => 'Admin\PropertyController#langAdd', 'as' => 'admin.property.lang_add']);
Route::get("property/lang/{id}", ['uses' => 'Admin\PropertyController#langDelete', 'as' => 'admin.property.lang_delete']);
Route::get('users/add', 'Admin\UsersController#add');
Route::get('content/add', 'Admin\ContentController#add');
});
And the new url rotation doesn't conflict any folder under the public folder.
Good works.

Laravel 4 - Route to subdomain except www

Currently I have a routing group to subdomains and another routing group to the main site, like this:
Route::group(array('domain' => '{subdomain}.mysite.dev'), function() {
// Subdomain routes
Route::get('/', array('as' => 'home', 'uses' => 'SubdomainController#showHome'));
});
// Main
Route::get('/', array('as' => 'home', 'uses' => 'PublicController#showHome'));
It's working fine so far for both 'mysite.dev' and for 'stuff.mysite.dev' or any other subdomain.
The problem is that it assumes 'www' (as in 'www.mysite.dev') as a subdomain and I need it to be interpreted as the main site.
Thanks to itachi response, I came up with this:
Route::pattern('subdomain', '^((?!www).)*$');
Adding this line to the top of routes.php, makes the 'subdomain' match anything except 'www'.
Final form:
Route::pattern('subdomain', '^((?!www).)*$');
Route::group(array('domain' => '{subdomain}.mysite.dev'), function() {
// Subdomain routes
Route::get('/', array('as' => 'home', 'uses' => 'SubdomainController#showHome'));
});
// Main site routes (works for mysite.dev and www.mysite.dev)
Route::get('/', array('as' => 'home', 'uses' => 'PublicController#showHome'));
Route::pattern('subdomain','dev|test|mobile'); <---- add your subdomains
Route::group(array('domain' => '{subdomain}.mysite.dev'), function() {
// Subdomain routes
Route::get('/', array('as' => 'home', 'uses' => 'SubdomainController#showHome'));
});
// Main
Route::get('/', array('as' => 'home', 'uses' => 'PublicController#showHome'));
just add the 1st line and you are done!
1) You could use .htaccess redirect www to non-www
2) check http://laravel.com/docs/routing for Regular Expression constraints, so at the end of the route you add ->where('subdomain', '!www'); ( not tested )

Categories