Yii URL Manager: problems in create an view url - yii

in file config main.php:
'/view-details/<slug:[a-zA-Z0-9]+>' => array('product/details', 'urlSuffix' => '.html')
create url:
$this->createUrl('product/details', array('slug'=>'my-product-demo-with-id-123'));
result:
mydomain/view-details/my-product-demo-with-id-123.html
(done! perfect generate url)
but, when visit link, this is error: unsolved request view-details/my-product-demo-with-id-123.html
if remove all character "-" (mydomain/view-details/MyProductDemoWithID123.html), it's working, not error.
what is problems? somebody can help me?

The regular expression you're using will only match single words but your url has hyphens as well.
You need to have a rule that looks like this
'/view-details/<slug:[-\w]+>' => array('product/details', 'urlSuffix' => '.html')

I'm not sure (new at Yii!) but probably this can be related to a bad method in your Controller actionView().
Look if you have in your controller "View-details" a method similiar to "actionMyProductDemoWithID123()" and inside of this, you render that file.
Example: yourDomain/yourController/yourView
Class YourController{
actionYourView(){
$this->render('yourView');
}
}
In your case, you probably have something similar to this:
Class view-details{
actionMyProductDemoWithId123(){
$this->render('MyProductDemoWithID123');
}
}
If it's not, you can take a look at this post: http://www.yiiframework.com/wiki/404/hyphenation-of-routes-in-url-management/

You missed the "-" part of your regex so it won't match try
'/view-details/<slug:[a-zA-Z0-9\-]+>' => array('product/details', 'urlSuffix' => '.html')

In your regex you are allowing only alphanumeric values. Your have to allow "-" also as you are using in your parameter(slug).
Regex with Alphanumeric and -,_
'view-details/<slug:[a-zA-Z0-9-_]+>' => array('product/details','urlSuffix' => '.html'),

Related

Laravel Queries / Controller Edits

So I am pretty new to Laravel, and I have spent the whole day fishing through various documentations but I am stuck on the way queries work within the actual application. Right now, I am trying to get some data in my database to display, and I looked at the query builder so that's where I am right now. I am also using a CRUD based admin panel for entry in the database. And since it is CRUD based, it has created the model and the controller already, so I am wondering if I need to edit any of those files to get this to work. Here is what the public function index() has right now (Using Laraadmin):
$module = Module::get('Events');
if(Module::hasAccess($module->id)) {
return View('la.events.index', [
'show_actions' => $this->show_action,
'listing_cols' => $this->listing_cols,
'module' => $module
]);
} else {
return redirect(config('laraadmin.adminRoute')."/");
}`
Obviously, I am trying to display some data from this Events table into my blade view. From what I was reading, I understood (or I thought) that it would be something similar to this:
foreach ($module as $module) {
echo $module->id;
}
But, I keep getting an error that whatever variable I pass in the loop is undefined, although I thought it was in the controller. Right now my model is just returning the view as well. Any help with this is greatly appreciated, or even just an explanation of the relationships with queries in Laravel. Thanks!
A few things to check when this happens:
Change module to a known array. This tests if your data array is set up correctly:
'module' => [ '1', '2', '3' ], // previously $module
Within the blade, now write a simple loop, such as:
#foreach ($module as $m)
{{ $m }}
#endforeach
If this version does work, then you know you have something up with your $module variable.
If the above still doesn't work, try to simplify your view request (temporarily):
view('foo', ['a' => 555]);
Then in foo.blade.php, simply have:
hello {{ a }}
EDIT
If all this seems to be correct, then the data being fetched is probably wrong (so its not a view issue). Try $module = Module::all();
It seems like you are returning a view that doesn't exist. If what you have now was correct, it would be looking for resources/views/la/events/index.blade.php Try replacing that return view line with this:
return view('events', [ ... rest of your variables ]);
And just a side note, on your foreach statement, it's probably best to use two separate variable names... so something like:
foreach ($module as $element) { ...

Laravel 5.3 Route model binding with multiple parameters

Is it possible to have route model binding using multiple parameters? For example
Web Routes:
Route::get('{color}/{slug}','Products#page');
So url www.mysite.com/blue/shoe will be binded to shoe Model, which has color blue.
First of all, it would feel more natural to have a route like the following:
Route::get('{product}/{color}', 'Products#page');
and to resolve product by route binding, and just use the color parameter in the controller method directly, to fetch a list of blue shoes for example.
But let's assume that for some reason it's a requirement. I'd make your route a bit more explicit, to start with:
Route::get('{color}/{product}', 'Products#page');
Then, in the boot method of RouteServiceProvider.php, I would add something like this:
Route::bind('product', function ($slug, $route) {
$color = $route->parameter('color');
return Product::where([
'slug' => $slug,
'color' => $color,
])->first() ?? abort(404);
});
first here is important, because when resolving route models like that you effectively want to return a single model.
That's why I think it doesn't make much sense, since what you want is probably a list of products of a specific color, not just a single one.
Anyways, I ended up on this question while looking for a way to achieve what I demonstrated above, so hopefully it will help someone else.
Do not forget to declare the parameter types:
Route::delete('safedetail/{safeId}/{slug}', [
'as' => 'safedetail.delete',
'uses' => 'SafeDetailController#destroy',
])->where([
'safeId' => '[0-9]+',
'slug' => '[a-z]+',
]);
Try changing your controller to this:
class Pages extends Controller{
public function single($lang, App\Page $page){
dd($page);
}
}
You must add the Page Model.

How to write a URL rule in Yii framework?

<?php echo CHtml::link($value->title, array(Yii::app()->createUrl('forum/thread', array('id'=>$value->thread_id)))); ?>
i got a link
forum/thread/2
in my urlManager rules 'thread/<id:\d+>' => 'forum/thread',
how to change the rule and method createUrl?
createUrl('any-value/forum/thread', array('id'=>$value->thread_id))
to get in url
forum/any-value/thread/2 or forum/php-for-newbies/thread/2
I am sorry for my english, thanks a lot
URL Manager rule should look like this:
'forum/<title:\w+>/thread/<id:\d+>' => 'forum/thread', //make sure this is listed first so it has priority
'thread/<id:\d+>' => 'forum/thread',
Then in your controller you would have this:
public function actionThread($id,$title=null) {
//$title will contain title from url if sent
}
Try this:
'forum/any-value/thread/<id:\d+>' => 'any-value/forum/thread',
and with this:
createUrl('any-value/forum/thread', array('id'=>$value->thread_id))
So you should get forum/any-value/thread/2
that should work!
But If you are inside the module called forum then you would do like that:
'any-value/thread/<id:\d+>' => 'any-value/forum/thread',
and with this:
createUrl('any-value/forum/thread', array('id'=>$value->thread_id))

Escape attribute value / prevent html sanitization in Haml

Here is my Haml:
%a{:href => "/settings", "data-icon" => "⚙"}
Which outputs:
<a data-icon='&#9881;' href='/settings'>Settings</a>
I want to output:
<a data-icon='⚙' href='/settings'>Settings</a>
So I've tried escaping the ampersand:
%a{:href => "/settings", "data-icon" => "\⚙"}
But it seems escapes only work for the first character of a line.
I've also tried different methods of interpolation/escaping with no success:
"#{"⚙"}"
Using plain html (Settings) is a stopgap measure that I do not consider a solution. What if I want to set the data-icon programatically?
Another option is:
%a{:href => "#", "data-icon" => "⚙"} Settings
But what if I want to use PUA characters? It also makes it a lot harder to tell what the character is in the markup.
Demo
You need to set the escape_attrs option to false or :once.
$ haml --no-escape-attrs
%a{:href => "/settings", "data-icon" => "⚙"}
output:
<a data-icon='⚙' href='/settings'></a>
--no-escape-attrs sets the escape_attrs option to false from the command line. See the docs for info on how to set options in other cases.
(It doesn’t look like codepen.io lets you specify Haml options, so I can’t provide a demo there).

URL parameter in the URL itself instead of after ? in Rails

I have the following line in the routes:
match 'area/:area_id/stores', :to => "stores_directory#index", :as => "stores_directory"
I have a form where I can select an area_id as:
<%= form_tag stores_directory_path, :method=>:get do %>
<select id="area_id" name="area_id">
....
</select>
This ads the area_id as a parameter after ?. So, my question is: How would I make it to add it in between area and stores?
I see one solution (not the best maybe, but also quite acceptable). It is possible to write your own middleware to handle this request, but this is the wrong way I believe (in this case).
Instead, use JS redirect
This could be written as a method (example using JQuery):
$('#area_id option').click(function() {
if (this.value) { // check value is not empty
document.location.href = Routes.stores_directory_path({area_id: this.value})
}
});
OR using options_for_select:
options = []
areas.each do |a|
options << [a.to_s, a.id, {:onclick => "document.location.href = Routes.stores_directory_path({area_id: #{a.id}})"}]
end
options_for_select(options)
Here I used js-routes gem for nice url's.
Hope this will be helpful.