Yii rules urlManager - yii

My URL Rule is as below :
'product-<typeproduct:.{1,255}>-prd-<positionIds:[\d\-]+>.html' => 'site/products/bycate',
I want to get positionIds in Array variable.
So is it possible to send positionIds in Array as below ? :
'product-<typeproduct:.{1,255}>-prd-<positionIds:array[\d\-]+>.html' => 'site/products/bycate',

As I understood you want to get it as array so url would be something like this:
'/product-2-prd-222,223,224.html'
Where 222,223,224 are positionId's that you want to obtain in array.
As of I know, this cannot be done without creating urlRule class which would look something like this:
class ProductsUrlRule extends UrlRule
{
public function parseRequest($manager, $request)
{
if (preg_match("/product-([0-9]+)-prd-([0-9,]+).html/",$request,$vars)===3) {
$typeProduct = $var[1];
$productIds = explode(',',$var[2]);
return [
'controller/action',
'typeproduct'=> $typeProduct,
'positionIds'=>$productIds
];
}
}
}
this will parse example url that I showed you above to something like this:
'?r=controller/action&typeproduct=2&positionIds[]=222&positionIds[]=223&positionIds[]=224'
Ofcourse you need to replace controller/action with your controller/action pair and later to validate fields.

Related

Laravel 9 accessors and mutators - Simple example not working

So I am trying to get my mutators and accessors to work in Laravel 9, in my Tag model I have the following:
protected function name(): Attribute
{
return Attribute::make(
get: fn ($value) => strtolower($value),
set: fn ($value) => strtolower($value),
);
}
When displaying the name in my blade view however, the name is not being displayed in lower cases ({{ $tag->name }}), also not when saving a new model to the database.
The following does work btw:
public function getNameAttribute($value)
{
return strtolower($value);
}
Also when using public it does not work:
public function name(): Attribute
Just trying to understand what I am doing wrong here?
I am using Laravel version 9.44
I dont know if the question's content is exactly your code. I had a similiar problem, get and set not working. But it worked in other model files.
I just found the solution once again, ya, once again.
If the attribute(column name) has two words, you have to make it together, first word should be lowercase.
short_name
protected function shortName(): Attribute
{
return Attribute::make(
get: fn ($value) => strtolower($value),
set: fn ($value) => strtolower($value),
);
}
Inject the class at top of page like below
use Illuminate\Database\Eloquent\Casts\Attribute;

method file returnsMethod Illuminate\Http\Response::file does not exist

i am trying to send a file with my API response to postman
return response($company)->file($company->logo, $company->main_photo);
laravel woops returns:
Method Illuminate\Http\Response::file does not exist.
what am i doing wrong?
I think you do not need to retrieve a file using the response helper method.
it just needs to send file location to the front-end, e.g. let assume your $company object shape is something like:
{
id: 1234,
name: 'My Company',
logo: 'images/companies/logo/1425.jpg'
}
then it is enough to pass above object to your front-end and in a contract ask your front end to put http://example.com/files/ at the beginning of file address then or you may define a JsonResource class and override the logo path with the absolute address (append base-URL to the beginning).
it might look like:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ComapnyResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request
* #return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'logo' => 'https://example.com/file/' . $this->logo,
];
}
}
Take a look the documentation.

Yii2 remove form name from url parameters

I use Yii2 forms and after submit my URL looks like:
http://example.com/recommendations/index?RecommendationSearch%5Bname%5D=&RecommendationSearch%5Bstatus_id%5D=&RecommendationSearch%5Btype%5D=0&RecommendationSearch%5Bcreated_at%5D=&sort=created_at
As you may seem each parameter contains form name RecommendationSearch. How to remove this RecommendationSearch from parameters in order to get URL url like the following:
http://example.com/recommendations/?name=&status_id=&type=0&created_at=&sort=created_at
You need to override formName() in your RecommendationSearch model to return empty string:
public function formName() {
return '';
}

Yii url routing

i have a url example.com/information/london. whenver some one calls this url i want to call a controller information and its index method. but i want to pass slug as jobs-in-london i.e. example.com/information/jobs-in-london how can i achive this by writing url rule in config/main.php.
i.e i want to redirect my page example.com/information/london to example.com/information/jobs-in-london but dont want to use .htaccess i want to achieve this only by url routing rules i have tried this by writing
'<_c:(information)>/<slug:london>'=>'information/index/jobs-in-london'
but this wont work for me.
class InformationController extends Controller
{
public function actionIndex($slug)
{
CVarDumper::dump($slug,10,true);
exit;
}
}
Your question is unclear. You may mean either of the following:
Url:
example.com/information/london
example.com/information/singapore
Rule:
'information/<slug>' => 'information/index',
Controller:
public function actionIndex($slug)
{
$slug = "jobs-in-".$slug;
var_dump($slug);
}
Slug Result:
jobs-in-london
jobs-in-singapore
Url:
example.com/information/jobs-in-london
example.com/information/jobs-in-singapore
Rule:
'information/<slug:jobs-in-.+>' => 'information/index',
Controller:
public function actionIndex($slug)
{
var_dump($slug);
}
Slug Result:
jobs-in-london
jobs-in-singapore
Here is what you will exactly have to do in the routes:
'<controller:(information)>/<slug:>' => '<controller>/index'
and here is how to add the slug while you're creating the URL:
Yii::app()->createUrl('information/index', array('slug' => 'jobs-in-london'));
and to check what the slug is, what you did to the function is correct.

How do I make a construct to have beforeAuth only apply to certain views/functions in Laravel 4

I have a resource in Laravel I have called artists with an ArtistsController. I would like to add filters to some of the pages, but not all. I know I can add a filter to all of the functions/views in the resource controller like so:
public function __construct()
{
$this->beforeFilter('auth', array('except' => array()));
}
How do I add the beforeAuth filter to only a certain view/function? I would like a user to be logged in in order to go the "index" view, but I would like a user to be able to go to the "show" pages without necessarily being logged in:
public function index()
{
$artists = Artist::all();
return View::make('artists.index', compact('artists'))
->with('artists', Artist::all())
->with('artists_new', Artist::artists_new());
}
public function show($id)
{
$artist = Artist::find($id);
return View::make('artists.show', compact('artist'))
->with('fans', Fan::all());
}
Is there a way to do this? Thank you.
Not sure if this helps but you could use the only key instead of the except (if I understand your question correctly).
$this->beforeFilter('auth', array('only' => array('login', 'foo', 'bar')));
Although that would still go in the constructor.