preg_match(): Compilation failed: invalid range in character class at offset 3 - laravel

My laravel Project Route invalid range in character class prolem please help me?
Route::get('{path}','HomeController#index')->where( 'path', '([A-z]+)?' )
Not solve
eRoute::group(['namespace' => 'Post'], function ($router) {
$router->pattern('id', '[0-9]+');
// $router->pattern('slug', '.*');
$router->pattern('slug', '^(?=.*)((?!\/).)*$');
// SingleStep Post creation
Route::group(['namespace' => 'CreateOrEdit\SingleStep'], function ($router) {
Route::get('create', 'CreateController#getForm');
Route::post('create', 'CreateController#postForm');
Route::get('create/finish', 'CreateController#finish');

I don't know what the intended routing pattern is supposed to be, but I speculate that it does not supports lookarounds. So instead of this:
$router->pattern('slug', '^(?=.*)((?!\/).)*$')
Try this:
$router->pattern('slug', '^[^/]*$');

Related

Why does the route name products not work?

I use Laravel with Vue.
I added the following route:
Route::patch('/products/updateOrder', 'Product\ProductController#updateOrder')->name('updateOrder');
I added it in the block:
Route::group([ 'namespace' => 'App\Http\Controllers\Api\BasicData', 'prefix' => 'basicData', 'middleware' => ['role:basicDataAdmin']], function () {
Route::patch('/products/updateOrder', 'Product\ProductController#updateOrder')->name('updateOrder');
Route::get('/products/{product}/productProcesses', 'Product\ProductProcessController#index');
Route::get('/products/{product}/productProcesses/{process}', 'Product\ProductProcessController#show');
Route::post('/products/{product}/productProcesses/{process}', 'Product\ProductProcessController#store');
Route::put('/products/{product}/productProcesses/{process}', 'Product\ProductProcessController#update');
Route::delete('/products/{product}/productProcesses/{process}', 'Product\ProductProcessController#destroy');
Route::resource('/products', 'Product\ProductController')->except(['updateOrder']);
Route::resource('/workplaces', 'Workplace\WorkplaceController');
Route::resource('/partNames', 'PartName\PartNameController');
Route::resource('/processes', 'Process\ProcessController');
});
I get the following error:
"message": "No query results for model [App\\Models\\BasicData\\Product\\Product] updateOrder",
Update order function:
public function updateOrder(Request $request)
{
Product::setNewOrder($request->productIds);
}
I tried to change the order of the routes. Same problem.
When i add an x to the name like this Route::patch('/productsx/updateOrder' it works.
I thought that it's the order of the routes. But it isn't.
Is the only way to solve it to change the name products?

Laravel 5.1 route not expecting array

I have the following routes:
use NexCast\Domain\NiveisServico\NiveisServicoController;
Route::group(['prefix' => 'niveis-servico', 'name' => 'niveis-servico.'], function () {
Route::get('/', [NiveisServicoController::class, 'getData'])->name('get');
Route::post('/', [NiveisServicoController::class, 'saveData'])->name('save');
Route::delete('/', [NiveisServicoController::class, 'deleteData'])->name('delete');
});
However I am receiving the following error:
Type error: ReflectionFunction::__construct() expects parameter 1 to be string, array given
What am I doing wrong?
The error you're getting happens because you're passing an array as the second parameter to the Route:: methods inside the group, but it expects a string instead. This is an example of what they should look like using your code:
Route::get('/', 'NiveisServicoController#getData')->name('get');
You can find more information about routes in the docs for the Laravel 5.1 version.

Laravel optional prefix routes with regexp

Is there a way to create routes with prefixes so I can have routes like this
/articles.html -> goes to listing Controller in default language
/en/articles.html -> goes to the same controller
/fr/articles.html -> goes to the same controller
My current problem is that by doing:
Route::group(['prefix=>'/{$lang?}/',function(){});
a route like this: /authors/author-100.html will match a prefix 'authors` , and for sure there is no language called "authors".
I use laravel 5.5
There doesn't seem to be any good way to have optional prefixes as the group prefix approach with an "optional" regex marker doesn't work. However it is possible to declare a Closure with all your routes and add that once with the prefix and once without:
$optionalLanguageRoutes = function() {
// add routes here
}
// Add routes with lang-prefix
Route::group(
['prefix' => '/{lang}/', 'where' => ['lang' => 'fr|en']],
$optionalLanguageRoutes
);
// Add routes without prefix
$optionalLanguageRoutes();
This should be sufficient using the where Regex match on the optional route parameter:
Route::get('/{lang?}, 'SameController#doMagic')->where('lang', 'en|fr');
You can do the same on Route Group as well, else having all the options as in this answer evidently works.
An update to show the use of prefix:
Route::group(['prefix' => '{lang?}', 'where' => ['lang' => 'en|fr']],function (){
Route::get('', 'SameController#doNinja');
});
As far as I am concerned this should be sufficient even when there is no lang as well as when there is one, just maybe this group could come before other routes.
You can use a table to define the accepted languages and then:
Route::group([
'prefix' => '/{lang?}',
'where' => ['lang' => 'exists:languages,short_name'],
], function() {
// Define Routes Here
});
Another working solution would be to create an array of langs and loop over it:
$langs = ['en', 'fr', ''];
foreach($langs as $lang) {
Route::get($lang . "/articles", "SomeController#someMethod");
}
For sure this makes your route file less readable, however you may use php artisan route:list to clearly list your routes.

Unused parameters in routes Laravel 5.1

I write api for mobile, since I have more than one version, I don't want to copy-paste the same routes, hence I decided to do something like this:
Route::model('callRequestNote', CallRequestNote::class);
Route::group([
'prefix' => 'api/v{version}/mobile',
'where' => ['version' => '[1|2]'],
], function () {
Route::delete('/notes/{callRequestNote}', MobileNotesController::class . '#destroy');
// other duplicated routes
});
public function destroy($version, CallRequestNote $callRequestNote)
{
//
}
I use version parameter only in BeforeMiddleware, hence I don't need it in controller. My question is how to replace those parameters and make CallRequestNote as a first argument and get rid of $version?

How to add dynamically prefix to routes?

In session i set default language code for example de. And now i want that in link i have something like this: www.something.com/de/something.
Problem is that i cant access session in routes. Any suggestion how can i do this?
$langs = Languages::getLangCode();
if (in_array($lang, $langs)) {
Session::put('locale', $lang);
return redirect::back();
}
return;
Route::get('blog/articles', 'StandardUser\UserBlogController#AllArticles');
So i need to pass to route as prefix this locale session.
If you want to generate a link to your routes with the code of the current language, then you need to create routes group with a dynamic prefix like this:
Example in Laravel 5.7:
Route::prefix(app()->getLocale())->group(function () {
Route::get('/', function () {
return route('index');
})->name('index');
Route::get('/post/{id}', function ($id) {
return route('post', ['id' => $id]);
})->name('post');
});
When you use named routes, URLs to route with current language code will be automatically generated.
Example links:
http://website.com/en/
http://website.com/en/post/16
Note: Instead of laravel app()->getLocale() method you can use your own Languages::getLangCode() method.
If you have more questions about this topic then let me know about it.
Maybe
Route::group([
'prefix' => Languages::getLangCode()
], function () {
Route::get('/', ['as' => 'main', 'uses' => 'IndexController#index']);
});

Resources