Okay after get almost every thing work in my code
and its pretty Good
i need help about how to validate the user url Input
and if he did insert a non Url how to foreword it to 404 page
and here is my Route file
Route::get('/', function()
{
return View::make('hello');
});
Route::post('/', function()
{
$url = Input::get('url');
$record = Url::where('urls', '=', $url)->first();
if ($record) {
return View::make('result')
->with('shortend', $record->shortend);
}
function get_uniqe_short_url()
{
$shortend = base_convert(rand(1000,99999), 10, 36);
if (Url::where('shortend', '=' ,$shortend)->first() ){
get_uniqe_short_url();
}
return $shortend;
}
$shortend = get_uniqe_short_url();
// otherwise add new row and return shortned url
$row = new URl;
$row->urls = $url;
$row->shortend = $shortend;
$row->save();
// create a results view and present the short url to the usr
if ($row){
return View::make('result')->with('shortend',$shortend);
}
});
Route::get('{shortend}', function($shortend)
{
// query the DB For the row with that short url
$row = Url::where('shortend', '=', $shortend)->first();
// if not found redirect to home page
if (is_null($row) ) return Redirect::to('/');
// else grab the url and redirect
return Redirect::to($row->urls);
});
forgive me for my many questions but i am totally new to laravel
First, as a tip, you have logic like this in your routes.php. A URL router is meant to take HTTP requests and "route" them to the correct controllers and methods. See the documentation to get started with controllers.
I am never a fan of "redirecting to a 404 page", instead I believe if a page isn't found it should display a 404 page there. To do this with laravel, you can call App::abort(404); which will kill the application and return a 404 status to the browser. You can take this a step further by "listening" to 404 errors and returning your own custom view:
App::missing(function()
{
return View::make('errors/404');
});
Let me know if you need more help validating a URL, but I think you should start by restructuring your code by using controllers. You can check this question for regular expressions to match URLs. This is how you would use a regular expression in PHP:
if(!preg_match('/expression/', $url)) {
App::abort(404);
}
Related
On my index page I have made a chatbox. There I have a form element in my Vue component which executes an inertia post via "UseForm".
The problem I have is, because I make the post on "/chatbox/send", that I have to make a redirect to "index/home" afterwards, because otherwise the URL-header remains "/chatbox/send", which I don't want.
In addition, the page automatically scrolls up again when rendering.
My question; How can I re-render the page so that the URL-header is set to "/home" again and the page does not scroll up ?
public function sendChatMessage(Request $request) {
$msg = $request->input('text');
$username = Auth::user()->only('name')['name'];
if($this->createChatMessage($msg, $username)) {
return redirect('index/home');
}
return false;
}
you have to define it during your route in your vuejs I suppose you use.
Normally it should look like this:
this.$inertia.post(route('chatbox.send'), {text: this.text}, { preserveState: false, preserveScroll: true })
preserveState: it is used to say if you want to refresh the page if you return the same route in your php controller.
preserveScroll: is used to say that you want to stay at the same scrolling as at the time of the send.
more information: https://inertiajs.com/manual-visits
And in your controller I would do:
public function sendChatMessage(Request $request) {
$msg = $request->input('text');
$username = Auth::user()->only('name')['name'];
if($this->createChatMessage($msg, $username)) {
return redirect()->route('home');
}
}
I have the next code:
Route::get('/{lang}/user/sort-by-{class}', function ($lang,$class) {
return view('users.list',compact("lang","class"));
})->where('class', '[a-z]+');
Route::get('/{lang}/user/{user}', function ($lang,$user) {
return view('users.user',compact("lang","user"));
});
When condition in where is false, how can I send it directly to 404 when sort-by- is for example a number? The problem is that it goes to secondary route as an user.
For example:
/en/user/sort-by-name is ok
/en/user/sort-by-4446 must show 404 page
I know that i can do another route just between them with
Route::get('/{lang}/user/sort-by-{class}', function ($lang,$class) {
return \Response::view('errors.404',array(),404);
})->where('class', '.*');
but this seems ugly, i would like in same sentence
Basically, you may do this
Route::get('/{lang}/user/sort-by-{class}', function ($lang,$class) {
if (is_numeric($class)) {
abort(404, 'Your reason');
}
return view('heros.list',compact("lang","class"));
});
Though, using closures in routes is a bad practice because they cannot be serialized in production mode. That's why you should use a controller to return your view, and assign a middleware to this route which will check your class and abort the request if needed.
I am with a bit of a stuggle here. I managed to create the dynamic URL using the following code:
Home Page Controller
$satellites = DB::table('satellites')->get();
return view('pages/home', ['satellites' => $satellites]);
Blade File
#foreach($satellites as $satellite)
<li>{{$satellite->satname}}</li>
#endforeach
web.php
Route::get('{norad_cat_id}', 'Satellite#show');
Controller
public function show($norad_cat_id)
{
return view('pages/satellite');
}
The URL generated is: mysite.com/12345 (where 12345 is the norad_cat_id).
This code manages to create the dynamic URLs using the norad_cat_id from the database - which is what I want. The problem is that I can replace the URL with anything and it still creates a page (ie. replace the 12345 with something not from the database and a page is still created).
What I want is only for a URL to be generated only with the norad_cat_id and if there is no matching norad_cat_id in the database, display a 404 page.
In the show method add a fetch from database if there is no record just abort
public function show($norad_cat_id)
{
$satellite = DB::table('satellites')->where('norad_cat_id', $norad_cat_id)->first();
if( ! satellite){
return abort(404);
}
return view('pages/satellite');
}
PS: abort will automatically redirect to your resources/views/errors/404.blade.php
You can do this multiple ways
Create a regex for norad_cat_id
The example shows nummeric ([0-9]+)
Route::get('{norad_cat_id}', 'Satellite#show')->where(['norad_cat_id' => '[0-9]+']);
Use findOrFail() and on fail show the 404.
try
{
$user = Satellites::findOrFail($id);
return view('norad_cats');
}
// catch(Exception $e) catch any exception
catch(ModelNotFoundException $e)
{
return view('404');
}
You can throw 404 in your controlller (for example). Just check if records exists in database - if not then return error.
Example:
public function show($cat_id)
{
$sattelites = DB::table('sattelites')->where('norad_cat_id', $cat_id)->get();
if ($satellites === NULL)
{
\App::abort(404);
}
return view('pages/sattelite', [
'satellites' => $satellites
]);
}
I think you get the idea.
Here's an example using query builder which is what I assume you're using:
public function show($norad_cat_id)
{
$norad_cat_data = DB::table('satellites')->where('norad_cat_id', $norad_cat_id)->get();
if (!is_null($norad_cat_id)
{
return view('pages/satellite');
}
\App::abort(404);
}
I want to redirect the user for particular page after successful login.
I don't want the user to navigate to the last viewed page after login.
I have tried following url but its show me error.
Error:
$credentials are required.
Laravel redirect back to original destination after login
I have changed redirect page using following code after login
Previous
return Redirect::intended('home');
Change to
return Redirect::to('home');
Add auth filter to your route and add logic to redirect user if login is success or failure.
Your route will look something like:
Route::group(array('domain'=>'a.b.com', 'before'=>'auth'), function() {
and your filter will be like:
Route::filter('auth', function()
{
if (Auth::user()->guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('account/login');
}
}
});
Have a look here for adding route and filter, and here to get basic information regarding filter. These 2 tutorials will also help:
Link 1 & Link 2
In your AccountController, try to add something inside validate function:
if (Session::has('url.intended')) {
$url = Session::get('url.intended');
Session::forget('url.intended'); // unset referring url from session
return Redirect::to($url); // redirect to referring url
}
else {
return Redirect::to('/'); // redirect to home page
}
Customize redirect location by defining a redirectPath property on the Auth/AuthController:
protected $redirectPath = '/dashboard';
I'm currently trying to route as follows:
If user GETs /account/
If session has account_id, user is logged in; show his account information
If not, user is not logged in; show login/create form
If user POSTs /account/
If input has create, user wants to create account; create it
If not, user wants to login; find his account and go again to /account/
My routes are set this way:
Route::get('account', function() {
if (Session::has('account_id'))
return 'AccountsController#show';
else
return 'AccountsController#index';
});
Route::post('account', function() {
if (Input::has('create')) {
return 'AccountsController#create';
else
return 'AccountsController#login';
)};
This is somewhat how I would do with Rails, but I don't know how to point to the controller method. I just get the returned string as a result. I didn't find it in Laravel documentation (which I found really poor, or have I searched wrong?) neither in any other web tutorial.
Try the following:
Route::get('account', function() {
if (Session::has('account_id')) {
$action = 'show';
return App::make('AccountsController')->$action();
}
else {
$action = 'index';
return App::make('AccountsController')->$action();
}
});
Route::post('account', function() {
if (Input::has('create')) {
$action = 'create';
return App::make('AccountsController')->$action();
}
else {
$action = 'login';
return App::make('AccountsController')->$action();
}
)};
So, you want to put all your logic in the controllers.
You would want to do
Route::get('account', 'AccountsController#someFunction');
Route::get('account', 'AccountsController#anotherFunction');
Then, in the respective controllers, you would want to make your tests, then do, for example,
if (Session::has('account_id')){
return Redirect::action('AccountsController#show');
}
else{
return Redirect::action('AccountsController#index');
}
However, make sure you define a route for each action, so your application can detect a URL.
For example, if you want to have
Redirect::action('AccountsController#show')
you will need to define this action like:
Route::get('/account/show', 'AccountsController#show')